简体   繁体   中英

Telephone number validator

Hi everybody i'm working on the telephone number project i have tried many regex pattern to match the cases but i am still stuck

The user may fill out the form field any way they choose as long as it has the format of a valid US number. The following are examples of valid formats for US numbers (refer to the tests below for other variants):

555-555-5555

(555)555-5555

(555) 555-5555

555 555 5555

5555555555

1 555 555 5555

this is what i tried

1([(\s]+\d{3}[)\s])(\d+)$|([\s(\d]\d+[\s|)|-\d])(\d+[\s-\d])+(\d+)$

the validator should return false for any case that don't match those above like: 10 (757) 622-7382 or 555)-555-5555

^(1 )?(\d{3}|\(\d{3}\))[\- ]?\d{3}[\- ]?\d{4}$

^                 start of string
(1 )?             optional "1 "
(
    \d{3}            three digits
    |                or
    \(\d{3}\)        three digits wrapped in parenthasis
)
[\- ]?            optional "- " charactor
\d{3}             three digits
[\- ]?            optional "- " charactor
\d{4}             four digits
$                 end of string

You can see it running here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM