I want to limit the user to enter a special character in the number field on an Android device. "." (period) does not skip values โโin the onkeypress event as expected.
With <input type="text">I can block the user from entering any special character.
But with <input type="number"/>I can not limit the user to enter a keyword.
Tried a lot of stack overflow messages. But still no luck.
Below is the code I'm trying:
HTML
<input type="number" ng-model="registrationField.mobilenumber" tabindex="0" no-special-character="^[0-9-]*$" >
Js
app.directive("noSpecialCharacter", [function() {
return {
restrict: "A",
link: function(scope, elem, attrs) {
var limit = parseInt(attrs.limitTo);
var regex = RegExp(attrs.noSpecialCharacter);
angular.element(elem).on("keydown", function(e) {
if(e.key == "Unidentified"){
e.preventDefault();
return false;
}
});
}
}
}])
I use the event keydownbecause the event keypressdoes not fire when I press the dot key on the numeric keypad.
, e.key Unidentified, . .
, , .
.
Android Nexus 5X.