JavaScript Phone Number Validator
Requirements
- Check whether the given string has the format of a valid US phone number.
- The area code is required. If the country code is provided it must be equal to "1".
Implementation
telephoneCheck(str)
checks whether the given string str
has the format of a valid US phone number. Returns true
if the string is a valid US phone number, false
otherwise.
Example Usage
Use the browser console to interact with the function (Ctrl + Shift + I
, then select the "Console" tab):
> telephoneCheck("555-555-5555"); // true
> telephoneCheck("1 555-555-5555"); // true
> telephoneCheck("1 (555) 555-5555"); // true
> telephoneCheck("555-5555"); // false
> telephoneCheck("5555555"); // false
> telephoneCheck("1 555)555-5555"); // false