Last active
July 10, 2021 05:22
-
-
Save joaotanaca/00e32159490328338847b07cec0f86f0 to your computer and use it in GitHub Desktop.
Máscara de CPF e CNPJ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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