Skip to content

Instantly share code, notes, and snippets.

@guilhermeabell
Created October 23, 2022 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilhermeabell/c150fe73edc0cfa09186c407299899e5 to your computer and use it in GitHub Desktop.
Save guilhermeabell/c150fe73edc0cfa09186c407299899e5 to your computer and use it in GitHub Desktop.
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