Skip to content

Instantly share code, notes, and snippets.

@rfprod
Last active April 22, 2017 15:52
Show Gist options
  • Save rfprod/8c6415145d187c10e643 to your computer and use it in GitHub Desktop.
Save rfprod/8c6415145d187c10e643 to your computer and use it in GitHub Desktop.
Telephone Number Validator
function telephoneCheck(str) {
// valid patterns
var p01 = new RegExp('^[1]\\s\\d\\d\\d-\\d\\d\\d-\\d\\d\\d\\d$');
var p02 = new RegExp('^[1]\\s\\d\\d\\d\\s\\d\\d\\d\\s\\d\\d\\d\\d$');
var p03 = new RegExp('^[1]\\s\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d$');
var p04 = new RegExp('^[1]\\(\\d\\d\\d\\)\\d\\d\\d-\\d\\d\\d\\d$');
var p05 = new RegExp('^[1]\\s\\(\\d\\d\\d\\)\\s\\d\\d\\d-\\d\\d\\d\\d$');
var p1 = new RegExp('^\\d\\d\\d-\\d\\d\\d-\\d\\d\\d\\d$');
var p2 = new RegExp('^\\d\\d\\d\\s\\d\\d\\d\\s\\d\\d\\d\\d$');
var p3 = new RegExp('^\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d$');
var p4 = new RegExp('^\\(\\d\\d\\d\\)\\d\\d\\d-\\d\\d\\d\\d$');
var p5 = new RegExp('^\\(\\d\\d\\d\\)\\s\\d\\d\\d-\\d\\d\\d\\d$');
// validator
if (str.match(p01) || str.match(p02) || str.match(p03) || str.match(p04) || str.match(p05) || str.match(p1) || str.match(p2) || str.match(p3) || str.match(p4)){return true;}else{return false;}
}
telephoneCheck("555-555-5555");

Telephone Number Validator

Function returns true if the passed string is a valid US phone number, otherwise false. If the country code is provided, function checks if the country code is 1. The following are all valid formats for US numbers: 555-555-5555 (555)555-5555 (555) 555-5555 555 555 5555 5555555555 1 555 555 5555

A script by V.

License.

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