1

I was working on some validations and I need to trigger some functions on onkeyup, onkeydown and onpaste events. However it is not working on Android mobile but working fine on the desktop.

function limitInput(event, length) {
  const e = event;
  if (
    [8, 9, 13, 37, 38, 39, 40].includes(e.keyCode) ||
    (e.keyCode >= 48 && e.keyCode <= 58) ||
    (e.keyCode >= 96 && e.keyCode <= 105) ||
    ((e.ctrlKey === true || e.metaKey === true) && (e.keyCode === 65 || e.keyCode === 67 || e.keyCode === 86))) {
      /* Allow it */
  } else {
    /* No valid keyCodes */
  }
  return true;
}
<input type="text" class="form-control" onkeydown="limitInput(event, 4)" onkeyup="formatInput(event, 4, '/', 2)" onpaste="formatInput(event, 4, '/', 2, true)" required>

Could anyone please help me with this issue. Thanks in advance!

panda_heart_3
  • 227
  • 2
  • 17
  • It is not working on touch device, because touch devices doesn't have a Keyup or down. You should probably use an onchange or oninput listener. Look here for further Event explanation https://stackoverflow.com/questions/38989559/jquery-keyup-event-for-mobile-device – Florian de Ville May 03 '19 at 15:57
  • @FloriandeVille, No its working fine on iOS devices. Only the issue is with the android devices. – panda_heart_3 May 03 '19 at 17:14

1 Answers1

0

It's better to use e.code instead of e.keyCode;

Based on MDN KeyboardEvent.keyCode:

You should avoid using this if possible; it's been deprecated for some time. Instead, you should use KeyboardEvent.code

Replace it and see whether it's working or not. More info KeyboardEvent.code

Reza
  • 3,473
  • 4
  • 35
  • 54
  • Can you please help me showing , how to use event.code for numeric validations? – panda_heart_3 Apr 17 '19 at 12:28
  • KeyboardEvent: key='s' | code='KeyS' KeyboardEvent: key='s' | code='KeyS' KeyboardEvent: key='d' | code='KeyD' KeyboardEvent: key='2' | code='Digit2' KeyboardEvent: key='2' | code='Digit2' KeyboardEvent: key='3' | code='Digit3' KeyboardEvent: key='4' | code='Digit4' KeyboardEvent: key='5' | code='Digit5' – Reza Apr 17 '19 at 12:36
  • More info [KeyboardEvent.code](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code) – Reza Apr 17 '19 at 12:37