I have the following code to allow only numeric
values to be entered in a form's input field:
HTML:
<input type="text" name="qtty" size="2" value="1" class="numOnly" />
Javascript:
var allowedKeys = [8, 9, 13, 27, 37, 39,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57
];
// 8 = backspace; 9 = tab; 13 = enter; 27 = escape; 37 = left; 39 = right;
// 48-57 = 0-9
$(".numOnly").keypress(function(event) {
var charCode = (event.which) ? event.which : event.keyCode;
if ($.inArray(charCode, allowedKeys) == -1)
return false;
else
return true;
});
This works on the desktop (Chrome, Firefox)
but not on Android (Chrome)
. On the latter some keystrokes including all alphanumerics (both upper and lower case
) are passed on to the textbox, as well as some special characters (e.g. $, %
) while other special characters (e.g. &, @
) are not.
Why is this, an what can I do to make the above code work on mobile platforms as well?