I am using angular js and below is my code which allows only numbers separated by comma.But regex which I used allows user to enter 1,,,2.
Is there any way so that I can restrict user to enter only one comma after each number?
Plunker - http://plnkr.co/edit/g1X9ldVuH4ZewAlumM1P?p=preview
function NumberValidator() {
return {
require: '?ngModel',
link: function(scope, element, attrs, ngModelCtrl) {
if (!ngModelCtrl) {
return;
}
ngModelCtrl.$parsers.push(function(val) {
var clean = val.replace(/[^\d+(,\d+)*$]/g, '');
if (val !== clean) {
ngModelCtrl.$setViewValue(clean);
ngModelCtrl.$render();
}
return clean;
});
element.bind('keypress', function(event) {
if (event.keyCode === 32) {
event.preventDefault();
}
});
}
};