Last active
September 22, 2024 13:15
-
-
Save alangabrielbs/bc5dc33d55742f4f025f0aefb2d73658 to your computer and use it in GitHub Desktop.
masks js
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
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