Skip to content

Instantly share code, notes, and snippets.

@JeffOgah
Created February 22, 2019 18:23
Show Gist options
  • Select an option

  • Save JeffOgah/9e45a967266f76e52155d276e47df9a7 to your computer and use it in GitHub Desktop.

Select an option

Save JeffOgah/9e45a967266f76e52155d276e47df9a7 to your computer and use it in GitHub Desktop.
JavaScript Algorithms and Data Structures Projects: Telephone Number Validator
//Return true if the passed string looks like a valid US phone number.
//The area code is required. If the country code is provided, confirm that the country code is 1
function telephoneCheck(str) {
//^(1\s?)? - checks if country code is provided. Checks for possible spacing
//(\(\d{3}\)|\d{3})[\s\-]? - checks that area code is 3 numbers possibly enclosed in parenthesis and also matches space or hyphen after
//\d{3}[\s\-]?\d{4}$ //checks 7 digit number possibly broken into 3 - 4 format. Ensures str ends in number
const phoneNumRegex = /^(1\s?)?(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?\d{4}$/
return phoneNumRegex.test(str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment