1
Hello I’m new in the javascript field and I’m having a doubt, as I can pick up the value of the keys and block off strings, special characters and only accepts NUMBERS in my field.
My Code to the bottom
<input type="text" id="numero" placeholder="377273288328">
<script>
let numero = document.getElementById("numero");
numero.addEventListener("keypress", e=>{
if(e.keyCode>=48 && e.keyCode<=57){
// o codigo do 0 ao 9
}else{
//como eu bloqueio?
}
})
</script>
in the
else
placee.preventDefault();
to lock the default event by pressing the key– Costamilam
Validate input to receive numbers only with jquery?
– NoobSaibot
William Thank you very much solved my problem.
– Otávio Guilherme
You don’t even need a script for that, just set the field type to
number
....<input type="number" id="numero" placeholder="377273288328">
– NoobSaibot
I posted an answer on Cpf that validates according to the Federal Revenue rule, that is, only if it is a valid and true Cpf. If you are interested please see https://answall.com/questions/295564/comorvalidar-cpf-com-m%C3%A1scara-em-javascript/295566#295566
– user60252