Search code examples
regexphone-number

Regular expression for mobilenumber and phonenumber


Giving the data: 050359554 and 0478770213

I'm searching for a regular expression that will check if the number begins with 04. If it starts with 04, then it should be followed by a 6,7,8 or 9 and 7 digits. Otherwise if it starts with only a 0 and no 4 it should be followed by 8 digits.

My current regex is like

/^(04[6789]\d{7})$|^(0\d{8})$/

The problem with this one is when I enter the number 0478770213 it already says that it's ok at the input of 047877021. But that is not the case.


Solution

  • You can use this regex:

    ^0(?:4[6789]\d{7}|(?!4)\d{8})$
    

    RegEx Demo