37

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 allowed only after country code(only once). Only 10 digit number should be allowed after the country code. The country code should be optional. If country code doesn't exist, it should accept only 10 digit number. Regex should prevent any invalid number like (eg:+91 0000000000 or 0000000000).

The regex should accept numbers like

  • +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)
0

3 Answers 3

73

Satisfies all your requirements if you use the trick told below

Regex: /^(\+\d{1,3}[- ]?)?\d{10}$/

  1. ^ start of line
  2. A + followed by \d+ followed by a or - which are optional.
  3. Whole point two is optional.
  4. Negative lookahead to make sure 0s do not follow.
  5. Match \d+ 10 times.
  6. Line end.

DEMO Added multiline flag in demo to check for all cases

P.S. You really need to specify which language you use so as to use an if condition something like below:

// 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,}/)) )
5
  • would be great ,if you add the condition like starts with only 9 8 7 only Jun 11, 2017 at 17:59
  • How to add the option to accept 00 instead of + Sep 8, 2019 at 13:16
  • 2
    Corrected regexp for not accepting all zeros and all zeros with country code ^(?!0+$)(\+\d{1,3}[- ]?)?(?!0+$)\d{10}$ Mar 19, 2020 at 6:55
  • 1
    How can you modify your regex to also accept () parenthesis? /^(\+\d{1,3}[- ]?)?\d{10}$/ Aug 9, 2021 at 9:23
  • @ishivamjain - that regex does not seem to accept 00+country_code '0034665665665'.match(/^(?!0+$)(\+\d{1,3}[- ]?)?(?!0+$)\d{10}$/)
    – Inigo EC
    May 5, 2022 at 16:06
13

This regex is very short and sweet for working.

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

Ex: +910123456789 or 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

This is how this regex for mobile number is working.
+ sign is used for world wide matching of number.

if you want to add the space between than you can use the

[ ]

here the square bracket represents the character sequence and a space is character for searching in regex.
for the space separated digit you can use this regex

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

Ex: +91 0123456789

Thanks ask any question if you have.

0
11

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
----------------------------------------------------------------------
3
  • link is broken kindly confirm it.
    – Fasiha
    Nov 19, 2015 at 9:00
  • @Fasiha thanks. As the link is broken, I have added the explanation instead. Nov 19, 2015 at 10:36
  • Nice explanation !!
    – shivani
    Jun 18, 2019 at 6:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.