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;              });           }       }; }); 

https://jsfiddle.net/vorant/lzw0yoma/


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -