I'm trying to make an input field with only integers enabled but in Android Chrome keyboard I'm unable to prevent the user from entering '.' key. The keycode for '.' key is 229 and cancelable is also true which is required for event.preventDefault().
The code comes and calls e.preventDefault() but somehow it doesn't work only for mobile input field in Chrome only. Firefox and iOS Safari works well.
function checkNumeric(e) {
if ([46, 8, 9, 27, 13, 110].indexOf(e.keyCode) !== -1 ||
// Allow: Ctrl+A
(e.keyCode === 65 && e.ctrlKey === true) ||
// Allow: Ctrl+C
(e.keyCode === 67 && e.ctrlKey === true) ||
// Allow: Ctrl+X
(e.keyCode === 88 && e.ctrlKey === true) ||
// Allow: home, end, left, right
(e.keyCode >= 35 && e.keyCode <= 39)) {
// let it happen, don't do anything
return
}
// Ensure that it is a number and stop the keypress
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault()
}
}
<input type='tel' onkeydown='checkNumeric(event)'>