Issue
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 optional6
(?:
Non capture group for the alternation01[0-46-9]\d{7,8}
Match01
then a digit except5
and 7-8 digits|
Or0\d{8}
Match a0
and 8 digits
)
Close non capture group
Answered By - The fourth bird
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.