0
I was trying to make a simple and small code to validate input
of the kind text
. For this I used an event onkeydown
in the input
where it returns false or true, ex: onkeydown="return somenteLetras()"
; in the Javascript code, when triggering this function, it captures the keycode
of the typed key 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 its entry since before the locked key is typed a accent, for example: if in the function I only allow the letter to enter a
, me type b
, the keycode
will be different and then it will lock the letter entry b
, but if I type any accent before then type b
, it blocks the accent but allows entry of the b
.
Follow the code to understand 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 he only allows the letter a
, but if you press the button ´
and then a b
, for example, instead of him blocking ´b
it just blocks the accent and let the b
. I wish I knew how to fix this.
Unfortunately I would like to validate the characters before they enter the
input
not to get that appears and disappears letters, to look aesthetically more beautiful, I’ve tried to use the eventkeypress
also, it solved my problem but it does not capture any keyboard button like Backspace, so needed to remedy this problem through thekeydown
really, but thank you anyway– Matheus