正则表达式 Javascript 电话号码验证最小最大长度检查

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9664883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:23:23  来源:igfitidea点击:

Regex Javascript Phone number validation min max length check

javascriptregexvalidation

提问by Aditya P Bhatt

I want a very simple Regex for Javascript validation of phone number, which allows 10 digitsand checks min numbers should be 10 and max 12including - dash two timesfor ex. 123-123-1234

我想要一个非常简单的正则表达式,用于电话号码的 Javascript 验证,它允许10 digits和检查min numbers should be 10 and max 12包括- dash two timesex。123-123-1234

I have found some on internet, but none of them is working for min / max length.

我在互联网上找到了一些,但没有一个适用于min / max length.

Looking forward for quick response here.

期待在这里快速回复。

Thanks !

谢谢 !

回答by stema

You could do this

你可以这样做

/^(?!.*-.*-.*-)(?=(?:\d{8,10}$)|(?:(?=.{9,11}$)[^-]*-[^-]*$)|(?:(?=.{10,12}$)[^-]*-[^-]*-[^-]*$)  )[\d-]+$/

See it here on Regexr

在 Regexr 上看到它

(?!...)is a negative lookahead assertion

(?!...)是一个否定的前瞻断言

(?=...)is a positive lookahead assertion

(?=...)是一个积极的前瞻断言

^                                          # Start of the string
    (?!.*-.*-.*-)                          # Fails if there are more than 2 dashes
    (?=(?:\d{8,10}$)                   # if only digits to the end, then length 8 to 10
        |(?:(?=.{9,11}$)[^-]*-[^-]*$)  # if one dash, then length from 9 to 11
        |(?:(?=.{10,12}$)
            [^-]*-[^-]*-[^-]*$         # if two dashes, then length from 10 to 12
         )
    )
[\d-]+                                     # match digits and dashes (your rules are done by the assertions)
$                                          # the end of the string

回答by Juicy Scripter

What you asking for wouldn't be a simple regular expression and also may be solved without any use 'em.

您要求的不是简单的正则表达式,也可以在不使用它们的情况下解决。

function isPhoneNumberValid(number){
  var parts, len = (
    parts = /^\d[\d-]+\d$/g.test(number) && number.split('-'),
    parts.length==3 && parts.join('').length
  );
  return (len>=10 && len<=12)
}

For sure this may be a bit slower than using a compiled regex, but overhead is way minor if you not going to check hundreds of thousandths phone numbers this way.

当然,这可能比使用编译的正则表达式慢一点,但是如果您不打算以这种方式检查千分之十的电话号码,则开销很小。

This isn't perfect in any way but may fit your needs, beware however that this allow two dashes in any place excluding start and end of number, so this will return truefor string like 111--123123.

这在任何方面都不是完美的,但可能符合您的需求,但是请注意,这允许在除数字的开头和结尾之外的任何地方使用两个破折号,因此这将返回true类似111--123123.

回答by kirilloid

There's no simple way to do this with regexp, especially if you allow dashes to appear at some different points.

使用正则表达式没有简单的方法可以做到这一点,特别是如果您允许在某些不同的点出现破折号。

If you allow dashes only at places as in your example, then it would be ^\d{3}-?\d{3}-?\d{4}$

如果您只在示例中的地方允许破折号,那么它将是 ^\d{3}-?\d{3}-?\d{4}$

This one: ^[\d-]{10,12}$matches string with length from 10 to 12 and contains only digits and dashes, but it also will match e.g. -1234567890-.

这一个:^[\d-]{10,12}$匹配长度从 10 到 12 的字符串并且只包含数字和破折号,但它也会匹配 eg -1234567890-