Regex pattern ^(\+?6?01)[0|1|2|3|4|6|7|8|9]\-*[0-9]{7,8}$
in HTML5 input return error. I tested the regex, no errors on regex101 as well as in my php code. But in HTML5 it does not function as it be. My code:
<input class="mdl-textfield__input" name="mobile_number" type="text" pattern="^(\+?6?01)[0|1|2|3|4|6|7|8|9]\-*[0-9]{7,8}$">
Error:
textfield.js:146 Pattern attribute value ^(+?6?01)[0|1|2|3|4|6|7|8|9]-*[0-9]{7,8}$ is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /^(+?6?01)[0|1|2|3|4|6|7|8|9]-*[0-9]{7,8}$/: Invalid escape
Anyone can help me? Thanks in advance for any helps offered.
My tested regex:
Regex pattern ^(\+?6?01)[0|1|2|3|4|6|7|8|9]\-*[0-9]{7,8}$
in HTML5 input return error. I tested the regex, no errors on regex101. as well as in my php code. But in HTML5 it does not function as it be. My code:
<input class="mdl-textfield__input" name="mobile_number" type="text" pattern="^(\+?6?01)[0|1|2|3|4|6|7|8|9]\-*[0-9]{7,8}$">
Error:
textfield.js:146 Pattern attribute value ^(+?6?01)[0|1|2|3|4|6|7|8|9]-*[0-9]{7,8}$ is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /^(+?6?01)[0|1|2|3|4|6|7|8|9]-*[0-9]{7,8}$/: Invalid escape
Anyone can help me? Thanks in advance for any helps offered.
My tested regex: https://regex101./r/1WsVwo/1
Share Improve this question edited Jul 31, 2017 at 3:03 Nere asked Jul 31, 2017 at 2:17 NereNere 4,0975 gold badges35 silver badges75 bronze badges 10-
1
where does leading
-?
and trailing?
e from? – Jaromanda X Commented Jul 31, 2017 at 2:20 -
1
What is the goal?
[0|1|2|3|4|6|7|8|9]
is allowing pipes is that what you want? It could just be[0-9]
if you want numbers. Also html5 patterns are full string so the^$
should be removed. – chris85 Commented Jul 31, 2017 at 2:20 -
2
What is
\-
supposed to mean? There's no need to escape the hyphen. – Robby Cornelissen Commented Jul 31, 2017 at 2:21 - @JaromandaX I update my error. – Nere Commented Jul 31, 2017 at 2:22
-
3
[0|1|2|3|4|6|7|8|9]
is incorrect. You mean[012346789]
, which is better written as[0-46-9]
. – elixenide Commented Jul 31, 2017 at 2:23
5 Answers
Reset to default 9Just to clarify, the answer of @elixenide is good enough for this question. But I do have some improvement for the regex part. First, +60 / 60 is Malaysia country calling code, then it follow by 9/10 digit number. But only one kind of number having 10 digit number after country calling code while others is 9 digit. For example:
- 60 1112345678 (number start with 11 have 10 digit)
- 60 121234567 (number that not start with 11 have 9 digit)
- 60 151234567 (number start with 15 will is not a valid phone number, it is for Digi broadband user if not mistaken)
So this is my improvement on how the regex should be
pattern="^(\+?6?01)[02-46-9]-*[0-9]{7}$|^(\+?6?01)[1]-*[0-9]{8}$"
Here are the link to regex101, try it
My sample output in regex101
You have a few problems with your regex. The one causing the "invalid escape" error is that you have \-
, but you do not need to (and should not) escape the hyphen. You should just have -
. A proper version of your input
is:
<input class="mdl-textfield__input" name="mobile_number" type="text" pattern="^(\+?6?01)[0-46-9]-*[0-9]{7,8}$">
Here's a demo.
In that example, I've also replaced the group [0|1|2|3|4|6|7|8|9]
with the cleaner and more accurate [0-46-9]
. In a character group (like [...]
), the pipe symbol (|
) is just another character, with no special meaning. So, for example, [0|1]
doesn't just match 0
or 1
; it also matches a literal |
character, which is not what you wanted. You might find this post helpful: Reference - What does this regex mean?
Tested further, below regex enforce user to key in dash (-
):
^(\+?6?01)[02-46-9][-][0-9]{7}$|^(\+?6?01)[1][-][0-9]{8}$
Enter phone number in 01Z-XXXXXXX
or 011-XXXXXXXX,
where X
is 0 to 9 and Z
is 0, 2, 3, 4, 6, 7, 8, 9
because your syntax error, invalid escape - => -
this correct:
<input class="mdl-textfield__input" name="mobile_number" type="text" pattern="^(\+?6?01)[0|1|2|3|4|6|7|8|9]-*[0-9]{7,8}$">
<input class="mdl-textfield__input" name="mobile_number" type="text" pattern="(\+?6?01)[0-9]{7,8}">
i change your pattern to (\+?6?01)[0-9]{7,8}
*Update: (\+?6?01)[0-46-9]-*[0-9]{7,8}
See demo