2
I’m trying to solve a problem where the user entered apenas
the Cpf numbers in a input
and, next to it, it will be displayed in the correct format of CPF
(xxx.xxx.xxx-xx).
However, someone knows why the score appears, but after I keep typing the score goes away?
var inCPF = document.getElementById("inCPF");
var outCPF = document.getElementById("outCPF");
function formatar() {
var cpf = inCPF.value;
if (cpf.length == 3 || cpf.length == 7) {
cpf += ".";
} else if (cpf.length == 11) {
cpf += "-";
}
outCPF.textContent = cpf;
}
inCPF.addEventListener("keyup", formatar);
It is possible to do this without using Regexp?
– Caio Ribeiro
I don’t know any other way to format the number while typing without Regexp or jQuery. But if you want to do the formatting after typing the full number, there are several other ways.
– Lucas Módolo
Details:
[\d]
is the same as\d
(usually when there is only one element between brackets, it is still, but there is no advantage to use the brackets so use without:\d{3}
, etc), and instead of[^0-9]
can use\D
– hkotsubo
is accepting something like 123.456.789-01234
– user60252
I edited according to your suggestions.
– Lucas Módolo