1
I wanted a simple input mask, just to put 999.999.999-99, dots and the last dash in the field of Cpf, no check, no nothing, simple, and no plugin just code, if I’m not mistaken have the regrex or something similar, I appreciate the attention!
1
I wanted a simple input mask, just to put 999.999.999-99, dots and the last dash in the field of Cpf, no check, no nothing, simple, and no plugin just code, if I’m not mistaken have the regrex or something similar, I appreciate the attention!
4
You can use this, but it only works with blur
:
var cpf = document.querySelector("#cpf");
cpf.addEventListener("blur", function(){
if(cpf.value) cpf.value = cpf.value.match(/.{1,3}/g).join(".").replace(/\.(?=[^.]*$)/,"-");
});
<input type="text" id="cpf" maxlength="11" />
It also works while the CPF is being typed:
function mascara(i){
var v = i.value;
if(isNaN(v[v.length-1])){ // impede entrar outro caractere que não seja número
i.value = v.substring(0, v.length-1);
return;
}
i.setAttribute("maxlength", "14");
if (v.length == 3 || v.length == 7) i.value += ".";
if (v.length == 11) i.value += "-";
}
<input oninput="mascara(this)" type="text">
Thank you! both work right!
I made a small correction on the first. Abs!
Browser other questions tagged javascript html
You are not signed in. Login or sign up in order to post.
take a look: https://answall.com/questions/199264/como-faco-uma-mascara-para-um-input
– aa_sp
Thanks for your help!
– Froslass