Skip to content

Instantly share code, notes, and snippets.

@Gennnji
Created April 30, 2020 12:45
Show Gist options
  • Save Gennnji/abd0a19edff7275422f7963a7f53b4d8 to your computer and use it in GitHub Desktop.
Save Gennnji/abd0a19edff7275422f7963a7f53b4d8 to your computer and use it in GitHub Desktop.
Phone Mask
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