Skip to content

Instantly share code, notes, and snippets.

@daihuaye
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save daihuaye/3df738c4b8e278270fc1 to your computer and use it in GitHub Desktop.

Select an option

Save daihuaye/3df738c4b8e278270fc1 to your computer and use it in GitHub Desktop.
formate phone number to format
formattedPhoneNumber: function formattedPhoneNumber(phone) {
var formats = '(...) ...-....',
formattedPhone = '',
phoneDigits;
if (!phone) {
return;
} else if (phone.substring(0, 1) === '+') {
phoneDigits = phone.substring(2);
} else {
phoneDigits = phone.replace(/[^0-9]/g, '');
}
for(prefix in formats) {
formatChar = formats[prefix];
if (formatChar === '.') {
if (phoneDigits.length === 0) {
return;
}
formattedPhone += phoneDigits.substring(0, 1);
phoneDigits = phoneDigits.substring(1);
} else if (phoneDigits.length > 0) {
formattedPhone += formatChar;
}
}
return formattedPhone;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment