I have a textfield on my angular js form for the Sale Price of an item. This field is not required, however I want it to be validated for valid number/decimal input > 0 if the user puts a value in the field. Any invalid input should force the field to revert back to 0.00M decimal.
I've tried a few different variations on my basic validNumber directive but I can't get it work. My directive works fine if I wanted the value to always be required for input.
Here's the directive I'm using now for the RetailPrice which is required. I've chopped out the code I added, since it wasn't working anyways.
app.directive('validNumber', function () {
return {
require: "ngModel",
link: function (scope, elm, attrs, ctrl) {
var regex = /^\d{1,3}(,\d{3})*(\.\d+)?$/;
var validator = function (value) {
ctrl.$setValidity('validNumber', regex.test(value));
return value;
};
ctrl.$parsers.unshift(validator);
ctrl.$formatters.unshift(validator);
}
};
});