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:

    Regex demo