Skip to content

Instantly share code, notes, and snippets.

@alangabrielbs
Last active September 22, 2024 13:15
Show Gist options
  • Save alangabrielbs/bc5dc33d55742f4f025f0aefb2d73658 to your computer and use it in GitHub Desktop.
Save alangabrielbs/bc5dc33d55742f4f025f0aefb2d73658 to your computer and use it in GitHub Desktop.
masks js
export const cpfMask = (value) => {
return value
.toString()
.replace(/\D/g, '')
.replace(/(\d{3})(\d)/, '$1.$2')
.replace(/(\d{3})(\d)/, '$1.$2')
.replace(/(\d{3})(\d{1,2})/, '$1-$2')
.replace(/(-\d{2})\d+?$/, '$1')
}
export const cnpjMask = (value) => {
return value
.toString()
.replace(/\D/g, '')
.replace(/(\d{2})(\d)/, '$1.$2')
.replace(/(\d{3})(\d)/, '$1.$2')
.replace(/(\d{3})(\d)/, '$1/$2')
.replace(/(\d{4})(\d)/, '$1-$2')
.replace(/(-\d{2})\d+?$/, '$1')
}
export const phoneMask = (value) => {
return value
.toString()
.replace(/\D/g, '')
.replace(/(\d{2})(\d)/, '($1) $2')
.replace(/(\d{4})(\d)/, '$1-$2')
.replace(/(\d{4})-(\d)(\d{4})/, '$1$2-$3')
.replace(/(-\d{4})\d+?$/, '$1')
}
export const cepMask = (value) => {
return value
.toString()
.replace(/\D/g, '')
.replace(/(\d{5})(\d)/, '$1-$2')
.replace(/(-\d{3})\d+?$/, '$1')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment