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!

2
  • 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 stackoverflow.com/questions/38989559/… Commented May 3, 2019 at 15:57
  • @FloriandeVille, No its working fine on iOS devices. Only the issue is with the android devices. Commented May 3, 2019 at 17:14

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Can you please help me showing , how to use event.code for numeric validations?
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'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.