Created
June 25, 2014 00:32
-
-
Save liambolling/30008dfc52a870d23123 to your computer and use it in GitHub Desktop.
A function to sort out letters, symbols, and anything beside numbers from phone numbers. Removes everything, but numbers from phone numbers. Used for numbers that are already entered in a database/array that need cleaning.
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
function cleanPhoneNumber(phoneString){ | |
//Use .match and regex to cut out the crap from the phone number | |
match = phoneString.match(/\D*\(?(\d{3})?\)?\D*(\d{3})\D*(\d{4})\D*(\d{1,8})?/); | |
//Assuming there are 3 parts... you can add more checks for arrays if you run into the issue of extensions. | |
if(match[3] === 'undefined'){ | |
if(match[2] === 'undefined'){ | |
if(match[1] === 'undefined'){ | |
} else { | |
//There is 1 part | |
finalMatch = match[1]; | |
} | |
} else { | |
//There are 2 parts | |
finalMatch = match[1] + match[2]; | |
} | |
} else { | |
//There are 3 parts | |
finalMatch = match[1] + match[2] + match[3]; | |
} | |
return finalMatch; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment