Allows Only Numbers to be Typed into a Text Box

Html

  1. <input type="text" name="number" only-Numbers>  
AngularJS Code
  1. .directive('onlyDigits', function()  
  2. {  
  3.     return   
  4.     {  
  5.         require: 'ngModel',  
  6.         restrict: 'A',  
  7.         link: function(scope, element, attr, ctrl)  
  8.         {  
  9.             function inputValue(val)  
  10.             {  
  11.                 if (val)  
  12.                 {  
  13.                     var digits = val.replace(/[^0-9.]/g, '');  
  14.                     if (digits !== val)  
  15.                     {  
  16.                         ctrl.$setViewValue(digits);  
  17.                         ctrl.$render();  
  18.                     }  
  19.                     return parseFloat(digits);  
  20.                 }  
  21.                 return undefined;  
  22.             }  
  23.             ctrl.$parsers.push(inputValue);  
  24.         }  
  25.     };  
  26. });