Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 77063

event.preventDefault() doesn't work with keyCode 229?

$
0
0

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(). Console.log output for . key mobile

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)'>

Viewing all articles
Browse latest Browse all 77063

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>