How to allow a number with digit limit and without decimal,and negative ina textfield using AngularJS directive -


here code.but not working in directive .please me show errors

when run enter characters(other number) in 2 times allowed. eg press 'w' in 2 times allwed in textfield. error in code?

here script

var myapp = angular.module('myapp', []);  myapp.directive('nksonlynumber', function () {   return {     restrict: 'ea',       require: 'ngmodel',             link: function (scope, element, attrs, ngmodel) {         scope.$watch(attrs.ngmodel, function(newvalue, oldvalue) {         var spiltarray = string(newvalue).split("");          if(attrs.allownegative == "false") {           if(spiltarray[0] == '-') {         newvalue = newvalue.replace("-", "");         ngmodel.$setviewvalue(newvalue);         ngmodel.$render();           }         }          if(attrs.allowdecimal == "false") {         newvalue = parseint(newvalue);         ngmodel.$setviewvalue(newvalue);         ngmodel.$render();         }          if(attrs.allowdecimal != "false") {           if(attrs.decimalupto) {          var n = string(newvalue).split(".");          if(n[1]) {             var n2 = n[1].slice(0, attrs.decimalupto);             newvalue = [n[0], n2].join(".");             ngmodel.$setviewvalue(newvalue);             ngmodel.$render();          }           }         }          if(attrs.limitdigit=="true"){          // attr.$set("ngtrim", "false");           var limitlength = parseint(attrs.awlimitlength, 10);// console.log(attrs);          if(ngmodel.$viewvalue.length>limitlength){           newvalue = ngmodel.$viewvalue.substring(0, limitlength);           ngmodel.$setviewvalue(newvalue );           ngmodel.$render();         }          }                        if (spiltarray.length === 0) return;         if (spiltarray.length === 1 && (spiltarray[0] == '-' || spiltarray[0] === '.' )) return;         if (spiltarray.length === 2 && newvalue === '-.') return;            /*check number or not.*/           if (isnan(newvalue)) {         ngmodel.$setviewvalue(oldvalue);         ngmodel.$render();           }       });       }   }; }); 

here textfield in html file

<b>numberlimited 4 digits</b><br> <input type="text" nks-only-number ng-model="mynumber6" aw-limit-length="4" allow-decimal="false" allow-negative="false" limit-digit="true" /><br>   

please use following directives

    /*     *directive numbers     *use - numbers-only     */      myapp.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 false;             }                         ngmodelctrl.$parsers.push(fromuser);         }         };     });      /*     *directive max legth prevent type     *use - my-maxlength     */      myapp.directive('mymaxlength', function() {       return {         require: 'ngmodel',         link: function (scope, element, attrs, ngmodelctrl) {           var maxlength = number(attrs.mymaxlength);           function fromuser(text) {           if (text.length > maxlength) {             var transformedinput = text.substring(0, maxlength);             ngmodelctrl.$setviewvalue(transformedinput);             ngmodelctrl.$render();             return transformedinput;           }            return text;           }           ngmodelctrl.$parsers.push(fromuser);         }       };      }) 

and use like

       <input name="field1"               type="text"                ng-model="model1"                numbers-only                my-maxlength="4"> 

hopes !


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -