0

I need to allow user to type in numbers only. Here is what I did.

Portion of my directive template:

<input type="text" maxlength="5" ng-keypress="allowNumbers($event)"> 

Function in my directive:

$scope.allowNumbers= function (ev) { var key = ev.charCode || ev.keyCode || 0; var allowed = ( key == 8 || key == 9 || key == 13 || key == 46 || key == 110 || key == 190 || (key >= 35 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105)); return allowed; }; 

While debugging, I noticed that even the function returns true or false, the value in textbox does get entered. Can I somehow prevent it for non-number key press?

5
  • Possible duplicate of Prevent typing non-numeric in input type number Commented Mar 18, 2016 at 10:14
  • Is your function named allowNumbers or allowNumberOnly? Commented Mar 18, 2016 at 10:15
  • try to use if (!allowed) ev.preventDefault(); Commented Mar 18, 2016 at 10:15
  • 1
    cant you just use type="number"? Commented Mar 18, 2016 at 10:16
  • Yeah @LDJ is right if you want only numbers why cant you use number input field rather than text input field? Commented Mar 18, 2016 at 10:24

3 Answers 3

4

You can use a directive:

angular.module('myApp', []).directive('numbersOnly', function(){ return { require: 'ngModel', link: function(scope, element, attrs, modelCtrl) { modelCtrl.$parsers.push(function (inputValue) { if (!inputValue) return '' var transformedInput = inputValue.replace(/[^0-9]/g, ''); if (transformedInput !== inputValue) { modelCtrl.$setViewValue(transformedInput); modelCtrl.$render(); } return transformedInput; }); } }; }); 

And use it like this:

<input type="text" maxlength="5" numbers-only/> 

You could use this behavior into any input needed just by adding the numbers-only attribute.

See working JSFiddle.

Sign up to request clarification or add additional context in comments.

2 Comments

Your solution is good, except for one thing. If the cursor is in the middle of the number, then type the letter (not numbers), the cursor moves to the end of the input.
Thanks for the answer
3

I'd approach this simpler and in a way that is guaranteed to catch more possible forms of input (e.g. copy-paste):

<input type="number" ng-model="myNumber"> $scope.$watch('myNumber', function (input) { $scope.myNumber = input.replace(/\D/g, ''); }); 

2 Comments

If the user copy and paste the data like 'asdd1as3asd3', the outcome as per the described logic will be 133. I don't think that is the intention of the query asked.
Why not? What should it do in that case? The input is only allowed to consist of numbers, stripping out all non-numbers seems entirely reasonable to me.
3
 function IsNumeric(e) { var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode; var ret = ((keyCode >= 48 && keyCode <= 57) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode)); // document.getElementById("error").style.display = ret ? "none" : "inline"; return ret; } 

and In html

 <input type="text" onkeypress="return IsNumeric(event);" /> 

1 Comment

Thanks for the answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.