Skip to content

Instantly share code, notes, and snippets.

@joaotanaca
Last active July 10, 2021 05:22
Show Gist options
  • Save joaotanaca/00e32159490328338847b07cec0f86f0 to your computer and use it in GitHub Desktop.
Save joaotanaca/00e32159490328338847b07cec0f86f0 to your computer and use it in GitHub Desktop.
Máscara de CPF e CNPJ
function maskCPF(element = new HTMLInputElement()) {
let cpfAtualizado;
const inputCpf = element;
const cpfAtual = element.value;
cpfAtualizado = cpfAtual.replace(
/(\d{3})(\d{3})(\d{3})(\d{2})/,
(_regex, one, two, three, four) => `${one}.${two}.${three}-${four}`,
);
inputCpf.value = cpfAtualizado;
return;
}
function maskCNPJ(element = new HTMLInputElement()) {
let cpfAtualizado;
const inputCpf = element;
const cpfAtual = element.value;
cpfAtualizado = cpfAtual.replace(
/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/,
(_regex, one, two, three, four, five) =>
`${one}.${two}.${three}/${four}-${five}`,
);
inputCpf.value = cpfAtualizado;
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment