html - Directive not working for input type=number using angularjs -
i have input type= number field.it allows whole number , decimal number.but need whole number enter in input.i have created directive working input type=text,not working input type=number.the reason need number keyboard in mobile.
below code number only,
angularjs :
.directive('numbersonly', function () { return { require: 'ngmodel', link: function (scope, element, attr, ngmodelctrl) { function fromuser(text) { if (text) { var transformedinput = text.replace(/[^0-9-]/g, ''); if (transformedinput !== text) { ngmodelctrl.$setviewvalue(transformedinput); ngmodelctrl.$render(); } return transformedinput; } return ''; } ngmodelctrl.$parsers.push(fromuser); } }; })
html:
<input type="number" ng-model="points" numbers-only required>
any suggestions welcome
try
html
<input type="text" ng-model="mytext" name="inputname" numbers-only>
directive
myapp.directive('numbersonly', function(){ return { require: 'ngmodel', link: function(scope, element, attrs, modelctrl) { modelctrl.$parsers.push(function (inputvalue) { var transformedinput = inputvalue ? inputvalue.replace(/[^\d.-]/g,'').replace('.','') : null; if (transformedinput != inputvalue) { modelctrl.$setviewvalue(transformedinput); modelctrl.$render(); } return transformedinput; }); } }; });
Comments
Post a Comment