Search code examples
javascriptjqueryregexvalidationpreg-match

correct the mobile regex pattern


I have a mobile validation regex that currently supports such input text:

+91xxxxxxxxxx
0091xxxxxxxxxx

and is valid for 10 digit mobile number starting with 9 and 8.

Can anyone modify the below regex so that input can start with any number from 1-9 and can also support country code format, e.g. 91xxxxxxxxxx fail to work exaclty as I want.

Here is the regex I tried:

^((\+|00)\d{1,3})?[89]\d{9}$

Solution

  • You can use this updated regex:

    ^((\+|00)?\d{1,3})?[1-9][0-9]{9}$
    

    See demo

    Changes made:

    • [89]\d{9} changed to [1-9][0-9]{9} to let the number start with 1 and the rest 9 digits can be any
    • (\+|00) changed to (\+|00)? so that we could omit checking for these leading symbols and validate numbers starting right with the country code.