-1
I’m doing a live validation of the CPF field entry, but I don’t want you to let letters in and do a "data mask"
cpf[0].onkeypress = function (e) {
var key = e.key
for(let i = 0 ; i <cpf[0].value.length ;i++) {
if(cpf[0].value[i]==1 ||cpf[0].value[i]==2 ||cpf[0].value[i]==3 ||cpf[0].value[i]==4 ||cpf[0].value[i]==5 ||
cpf[0].value[i]==6 ||cpf[0].value[i]==7 ||cpf[0].value[i]==8 ||cpf[0].value[i]==9 ||cpf[0].value[i]==10 ||
cpf[0].value[i]=='.' || cpf[0].value[i]=='-') {
} else {
cpf[0].value="";
}
}
if(key==1 ||key==2 ||key==3 ||key==4 ||key==5 ||
key==6 ||key==7 ||key==8 ||key==9 ||key==10 ) {
if(cpf[0].value.length==3){
cpf[0].value+='.';
} else if(cpf[0].value.length==7) {
cpf[0].value+='.';
} else if(cpf[0].value.length==11) {
cpf[0].value+='-';
}
} else {
cpf[0].value="";
}
But it turns out that when the person type the letter he clears the entire field of the CPF and leaves the letter that the person typed, how can I fix it?
Hello. Already tried using onChange instead of onkeypress?
– Tiago_Albuquerque