Last active
August 29, 2015 14:09
-
-
Save daihuaye/3df738c4b8e278270fc1 to your computer and use it in GitHub Desktop.
formate phone number to format
This file contains hidden or 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
| 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