Regex for Mobile Number Validation

前端 未结 3 1846
别那么骄傲
别那么骄傲 2020-12-05 02:38

I want a regex for mobile number validation. The regex pattern should be such that it must accept + only in beginning and space(or -) should be al

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 03:20

    Try this regex:

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

    Explanation of the regex using Perl's YAPE is as below:

    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
    ----------------------------------------------------------------------
    

提交回复
热议问题