用于移动号码验证的正则表达式

时间:2014-03-13 12:31:12

标签: regex

我想要一个用于手机号码验证的正则表达式。正则表达式模式应该是这样的,它必须仅在开始时接受+并且仅在国家代码之后允许空格(或-)(仅一次)。在国家代码之后应该只允许10位数字。国家代码应该是可选的。如果国家代码不存在,它应该只接受10位数.Regex应该防止任何无效的数字(例如:+91 00000000000000000000)。

正则表达式应该接受像

这样的数字
+1 8087339090,

+91 8087339090, 

+912 8087339090,

8087339090 ,

08087339090 ,

+1-8087339090, 

+91-8087339090,

+912-8087339090,

+918087677876(Country code(2 digits) + 10 digits Mobile Number),

+9108087735454(Country code(3 digits) + 10 digits Mobile Number)


The regex should not accept numbers like

++51 874645(double successive +),

+71  84364356(double successive spaces),

+91 808 75 74 678(not more than one space),

+91 808-75-74-678(not more than one -),

+91-846363,

80873(number less than 10 digit),

8087339090456(number greater than 10 digit),

0000000000(all zeros),

+91 0000000(all zeros with country code)

3 个答案:

答案 0 :(得分:40)

如果您使用下面介绍的技巧

,请满足您的所有要求

正则表达式:/^(\+\d{1,3}[- ]?)?\d{10}$/

  1. ^行首
  2. +后跟\d+,后跟-,这是可选的。
  3. 整点二是可选的。
  4. 否定前瞻以确保0不跟随。
  5. 匹配\d+ 10次。
  6. 行结束。
  7. DEMO 在演示中添加m ultiline标志以检查所有案例

    P.S。您确实需要指定使用的语言,以便使用if条件,如下所示:

    // true if above regex is satisfied and (&&) it does not (`!`) match `0`s `5` or more times
    
    if(number.match(/^(\+\d{1,3}[- ]?)?\d{10}$/) && ! (number.match(/0{5,}/)) )
    

答案 1 :(得分:2)

试试这个正则表达式:

^(\+?\d{1,4}[\s-])?(?!0+\s+,?$)\d{10}\s*,?$

使用Perl的YAPE解释正则表达式如下:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  (                        group and capture to \1 (optional
                           (matching the most amount possible)):
----------------------------------------------------------------------
    \+?                      '+' (optional (matching the most amount
                             possible))
----------------------------------------------------------------------
    \d{1,4}                  digits (0-9) (between 1 and 4 times
                             (matching the most amount possible))
----------------------------------------------------------------------
    [\s-]                    any character of: whitespace (\n, \r,
                             \t, \f, and " "), '-'
----------------------------------------------------------------------
  )?                       end of \1 (NOTE: because you are using a
                           quantifier on this capture, only the LAST
                           repetition of the captured pattern will be
                           stored in \1)
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    0+                       '0' (1 or more times (matching the most
                             amount possible))
----------------------------------------------------------------------
    \s+                      whitespace (\n, \r, \t, \f, and " ") (1
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
    ,?                       ',' (optional (matching the most amount
                             possible))
----------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  \d{10}                   digits (0-9) (10 times)
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  ,?                       ',' (optional (matching the most amount
                           possible))
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

答案 2 :(得分:1)

此正则表达式非常简短,可以正常工作。

/^([+]\d{2})?\d{10}$/

例如:+910123456789或0123456789

-> /^ and $/ is for starting and ending
-> The ? mark is used for conditional formatting where before question mark is available or not it will work
-> ([+]\d{2}) this indicates that the + sign with two digits '\d{2}' here you can place digit as per country
-> after the ? mark '\d{10}' this says that the digits must be 10 of length change as per your country mobile number length

这是此手机号码正则表达式的工作方式。
+符号用于数字的全球匹配。

如果您想在之间添加空格,可以使用

[ ]

在这里,方括号表示字符序列,空格是用于在正则表达式中搜索的字符。
对于空格分隔的数字,您可以使用此正则表达式

/^([+]\d{2}[ ])?\d{10}$/

例如:+91 0123456789

谢谢你有任何疑问。