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?