简体   繁体   English

电话号码验证器

[英]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

(555) 555-5555 (555) 555-5555

555 555 5555 555 555 5555

5555555555 5555555555

1 555 555 5555 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对于与上述不匹配的任何情况,验证器应返回 false,例如:10 (757) 622-7382 或 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你可以看到它在这里运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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