Input for receiving cpf

1

Hello, I'm new to the javascript field and I'm having a question, how can I get the value of the keys and lock strings, special characters, and accepts only NUMBERS in my field.

My Code Down

<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>
    
asked by anonymous 18.06.2018 / 01:03

2 answers

1
<input type="text" id="numero" placeholder="377273288328">
<script>
    let numero = document.getElementById("numero");
    numero.addEventListener("keypress", e=>{
        if(e.keyCode>=48 && e.keyCode<=57){
           return true;
        }else{
            e.preventDefault();// bloquea o evento padrão ao apertar a tecla
        }
    })
</script>
    
18.06.2018 / 01:20
0

You can only do return false to deny the key that was loaded.

a discussion of this recently here , although it was to block other characters.

    
18.06.2018 / 01:08