If the incoming string is a valid US phone number, it is returned true
.
Users can fill out a form with any valid US phone number. Here are some examples of valid numbers (as well as some variants to use when testing below):
555-555-5555 (555) 555-5555 (555) 555-5555555 555 555555555555551 555 555 5555
In this section you will see strings such as 800-692-7753
or 8oo-six427676;laskdjf
. Your task is to verify that the string given above is a valid US phone number. The area code is a must. If the country codes are given in the string, you must verify that they are 1
. Returns if the number is valid true
; Back false
.
It needs to be done using regular expressions.
function Telephonecheck (str) { var reg =/^1?? \d{3}|\ (\d{3}\)) [-]?\d{3}[-]?\d{4}$/; return Reg.test (str);}
Telephonecheck ("555-555-5555");
(\d{3}|\ (\d{3}\)) --555 or (555) of the form.
Validate US Telephone Numbers (Freecodemap advanced algorithm 1)