Search code examples
htmlregexpreg-matchtel

Regex for Malaysian mobile phone Number


This is the regex pattern i currently having for eg:01xxxxxxxx:

<input class="mdl-textfield__input" name="mobile_number" type="text" pattern="^(\+?6?01)[0-46-9]-*[0- 
9]{7,8}$">

I"m trying to allow not only 10 digits but also other formats as 11 digits(01xxxxxxxxx) and the office number eg:0xxxxxxxx.

Anyone can help me?


Solution

  • You can use an alternation | to match either there optional prefix part, omit matching the hyphens if you don't need them and as the second alternative match a 0 followed by 8 digits.

    The pattern attribute is implicitly anchored, so you can omit ^ and $

    \+?6?(?:01[0-46-9]\d{7,8}|0\d{8})
    

    The pattern matches:

    • \+?6? Match an optional + and an optional 6
    • (?: Non capture group for the alternation
      • 01[0-46-9]\d{7,8} Match 01 then a digit except 5 and 7-8 digits
      • | Or
      • 0\d{8} Match a 0 and 8 digits
    • ) Close non capture group

    Regex demo