Created
February 22, 2019 18:23
-
-
Save JeffOgah/9e45a967266f76e52155d276e47df9a7 to your computer and use it in GitHub Desktop.
JavaScript Algorithms and Data Structures Projects: Telephone Number Validator
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
| //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