I am creating a directive to restrict input box to accept numbers only. It should accept 1-9999999999, but no leading 0. It is working but after entering first number it is accepting all numbers and characters. Please help me to resolve this.it should accept 10, but not 01. For this my code is,
HTML:
<input type="text" ng-model="number" required="required" numbers-only="numbers-only" />
JS:
angular.module('myApp', []).directive('numbersOnly', function(){
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function (inputValue) {
if (inputValue == undefined) return ''
var transformedInput = inputValue.replace(/^[^1-9]*/g, '');
if (transformedInput!=inputValue) {
modelCtrl.$setViewValue(transformedInput);
modelCtrl.$render();
}
return transformedInput;
});
}
};
});
function MyCtrl($scope) {
$scope.number = ''
}
FIDDLE: FIDDLE