Skip to content

Instantly share code, notes, and snippets.

@yuritoledo
Created May 30, 2019 23:58
Show Gist options
  • Save yuritoledo/c75b976c776f6ad174261e9d74b7c728 to your computer and use it in GitHub Desktop.
Save yuritoledo/c75b976c776f6ad174261e9d74b7c728 to your computer and use it in GitHub Desktop.
export function cpfMask (value) {
return value
.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 function cnpjMask (value) {
return value
.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 function phoneMask (value) {
return value
.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 function cepMask (value) {
return value
.replace(/\D/g, '')
.replace(/(\d{5})(\d)/, '$1-$2')
.replace(/(-\d{3})\d+?$/, '$1')
}
export function pisMask (value) {
return value
.replace(/\D/g, '')
.replace(/(\d{3})(\d)/, '$1.$2')
.replace(/(\d{5})(\d)/, '$1.$2')
.replace(/(\d{5}\.)(\d{2})(\d)/, '$1$2-$3')
.replace(/(-\d{1})\d+?$/, '$1')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment