JavaScript Phone Number Validator

Requirements

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