Event listeners (onkeypress, onkeydown) on input not working in Android numeric keyboard
The keypress
event isn't fired, only keydown
and keyup
. onkeydown
you're able to invoke the preventDefault()
method. But what to prevent? charCode
is always 0
and keyCode
is 229
. You should check all the input.
Something like this:
if(!event.charCode) { this.removeInvalidChar(); return;}private removeInvalidChar(): void { setTimeout(() => { let input = this.control.control.value; if (input) { input.split('').forEach((char: string) => { if (!this.pattern.test(char)) { input = input.replace(char, ''); } }); this.control.control.setValue(input); } });}
What about this directive with $parsers.unshift
:
app.directive('format', ['$filter', '$window', function($filter, $window) { return { require: '?ngModel', link: function(scope, elem, attrs, ctrl) { if (!ctrl) return; scope.plainNumber = scope.test+''; ctrl.$parsers.unshift(function(viewValue) { if(!viewValue){ viewValue = scope.plainNumber; } scope.plainNumber = viewValue.replace(/[^\d]/g, ''); elem.val(scope.plainNumber); return scope.plainNumber; }); } };}]);
And usage:
<input type="number" ng-model="test" format />
Tested on Android
use inputType="numberSigned" it will provide you only numbers between 0-9 , atleast in native android it works flawlessly.