The function below tests the input of a cpf, (typing or pasting ) whose format must be ###.###.###-##
If the input consists only of numbers without the separators, or missing some tab, such as ###.########
how can I make the value
of the input to be converted to the valid format.
is not CPF validation but pure formatting !!!
function ValidaCPF(){
var ao_cpf=document.forms.form1.ao_cpf.value;
var cpfValido = /^(([0-9]{3}.[0-9]{3}.[0-9]{3}-[0-9]{2}))$/;
if (cpfValido.test(ao_cpf) == false) {
//alert("invalido");
var valorValido = document.getElementById("ao_cpf").value = "???????";
}
}
<form name="form1">
<input type="text" name="ao_cpf" id="ao_cpf" placeholder="CPF" maxlength="14" OnBlur="ValidaCPF();"/>
</form>
I will exemplify with numbers to be clear
-
When I enter
11111111111
it returns me in the input111.111.111-11
-
When I enter
111.11111111
it returns me in the input111.111.111-11
-
When I enter
111.111111-11
it returns me in the input111.111.111-11
-
etc ...