I was trying to make a simple, small code to validate input
of type text
. For this I have used an event onkeydown
in input
where it returns false or true, ex: onkeydown="return somenteLetras()"
; in the JavaScript code, by triggering this function, it captures the keycode
of the key typed and, depending on its code, it returns false or true allowing only letters to return true, at least in theory. The problem was when I realized that even if a key is locked, it allows it to enter it since before the key is pressed an accent is typed, for example: if in the function I only allow the input of the letter a
, I type b
, keycode
will be different and then it will block the input of b
, but if I type any accent before then enter b
, it blocks the accent but allows b
p>
Follow the code to understand it better:
HTML
<input type="text" onkeydown="return letraA()">
JS
function letraA() {
var kc = event.which || event.KeyCode;
if(kc == 65) {
return true;
}
return false;
}
In this code it only allows the letter a
, but if you hit the ´
key and then a b
, for example, instead of blocking ´b
it only blocks the accent and lets pass the b
. I wanted to know how to resolve this.