Skip to content

Instantly share code, notes, and snippets.

@earlonrails
Created July 28, 2011 17:40
Show Gist options
  • Save earlonrails/1112074 to your computer and use it in GitHub Desktop.
Save earlonrails/1112074 to your computer and use it in GitHub Desktop.
Format Phone numbers to US format with javascript
function strToAni(str) {
var regX = /(^[2-9]\d{0,2})\-?(\d{0,3})\-?(\d{0,4})$/;
var matches = regX.exec(str);
fstr = matches[1];
if (matches[2]){
fstr += "-" + matches[2];
}
if (matches[3]){
fstr += "-" + matches[3];
}
return fstr;
}
@earlonrails
Copy link
Author

easy as 1, 2, 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment