vendredi 18 septembre 2020

Input 4 Letter and 4 Number No Special Character only limit but allow function Keys?

I want to limit my text input to have only 4 letters then 4 numbers and no special character and no white space.

and code i wrote :

inputText.addEventListener("keydown", (e) => {
    if (e.key === "Enter") {
        if (!inputText.checkValidity()) {
            alert('Pattern not matched!!')
            return;
        }
    }
    let pos = e.target.selectionStart;
    pos < 4 ? /^[A-Z]$/.test(e.key) ? '' : e.preventDefault() : /^[0-9]$/.test(e.key) ? '' : e.preventDefault();

}) 

Now the problem is if i try to delete a input value from the field i can't as i prevented it with the current code i am only able to allow number and letters.

Now in order to allow them i have to do the Keycode based approach But keyCode is deprecated.

How to only allow 4 Letter > 4 Letter No white space No special character. And allow function button to work like backspace shift tab and all..




Aucun commentaire:

Enregistrer un commentaire