[英]Regex pattern for Malaysian mobile phone Number
Regex pattern ^(\\+?6?01)[0|1|2|3|4|6|7|8|9]\\-*[0-9]{7,8}$
in HTML5 input return error.正则表达式模式
^(\\+?6?01)[0|1|2|3|4|6|7|8|9]\\-*[0-9]{7,8}$
在 HTML5 输入返回错误。 I tested the regex, no errors on regex101.com as well as in my php code.我测试了正则表达式,在regex101.com和我的 php 代码中都没有错误。 But in HTML5 it does not function as it be.
但在 HTML5 中,它并没有像现在这样运行。 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
textfield.js:146 模式属性值 ^(+?6?01)[0|1|2|3|4|6|7|8|9]-*[0-9]{7,8}$ 不是一个有效的正则表达式:Uncaught SyntaxError: Invalid regular expression: /^(+?6?01)[0|1|2|3|4|6|7|8|9]-*[0-9]{7, 8}$/: 无效的转义
Anyone can help me?任何人都可以帮助我吗? Thanks in advance for any helps offered.
在此先感谢您提供的任何帮助。
My tested regex: https://regex101.com/r/1WsVwo/1我测试的正则表达式: https : //regex101.com/r/1WsVwo/1
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
正确版本是:
<input class="mdl-textfield__input" name="mobile_number" type="text" pattern="^(\+?6?01)[0-46-9]-*[0-9]{7,8}$">
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]
. 在该示例中,我还用更清晰,更准确的
[0-46-9]
替换了[0|1|2|3|4|6|7|8|9]
[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
; 因此,例如
[0|1]
不仅仅与0
或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?
您可能会发现这篇文章很有帮助: 参考-此正则表达式是什么意思?
Just to clarify, the answer of @elixenide is good enough for this question.只是为了澄清,@elixenide 的答案足以解决这个问题。 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.
首先,+60 / 60 是马来西亚国家电话代码,然后是 9/10 位数字。 But only one kind of number having 10 digit number after country calling code while others is 9 digit.
但只有一种号码在国家/地区代码后有 10 位号码,而其他号码是 9 位号码。 For example:
例如:
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这是regex101的链接,试试吧
My sample output in regex101我在regex101 中的示例输出
Tested further, below regex enforce user to key in dash (-): ^(+?6?01)[02-46-9][-][0-9]{7}$|^(+?6?01)[1][-][0-9]{8}$进一步测试,下面正则表达式强制用户输入破折号 (-): ^(+?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在 01Z-XXXXXXX 或 011-XXXXXXXX 中输入电话号码,其中 X 是 0 到 9,Z 是 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}$">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.