Created
April 30, 2020 12:45
-
-
Save Gennnji/abd0a19edff7275422f7963a7f53b4d8 to your computer and use it in GitHub Desktop.
Phone Mask
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
const getPurePhone = (value) => { | |
if (!value) { | |
return ''; | |
} | |
return value.replace(/\+7|[^\d]/g, '') | |
.replace(/^8(\d{10})$/, '$1'); | |
} | |
const phoneMask = (value) => { | |
value = getPurePhone(value); | |
const groups = value.match(/(\d{0,3})(\d{0,3})(\d{0,2})(\d{0,2})/); | |
groups[1] = groups[1].padEnd(3, '_'); | |
groups[2] = groups[2].padEnd(3, '_'); | |
groups[3] = groups[3].padEnd(2, '_'); | |
groups[4] = groups[4].padEnd(2, '_'); | |
return `+7 (${groups[1]}) ${groups[2]}-${groups[3]}-${groups[4]}`; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment