[英]How I use Regex pattern in HTML text input to validate phone number
My number format as shown in the below:我的号码格式如下图:
1. 775645645 (9 digits) 1. 775645645(9位)
2. 0775645645 (10 digits) 2. 0775645645(10位数字)
3. +94775645645 3. +94775645645
The numbers can start with 7 or 0 or +94.数字可以以 7 或 0 或 +94 开头。
So I tried it with regex pattern in HTML text input as shown in the below:所以我在 HTML 文本输入中尝试使用正则表达式模式,如下所示:
<input type="text" name="mobile" class="form-control" pattern ="(?:7|0|(?:\+94))[0-9]{9,10}$" required />
But this pattern is not working for me.但是这种模式对我不起作用。 Appreciate your help.感谢你的帮助。
UPDATE:更新:
Here Im using jQuery validate to validate a front end form.我在这里使用 jQuery 验证来验证前端表单。
You can use您可以使用
pattern="(?:7|0\d|\+94\d)\d{8}"
See the regex demo .请参阅正则表达式演示。 It will be compiled into a pattern like ^(?:(?:7|0\d|\+94\d)\d{8})$
and will match它将被编译成类似^(?:(?:7|0\d|\+94\d)\d{8})$
的模式并将匹配
^(?:
- start of string and the non-capturing group start ^(?:
- 字符串的开始和非捕获组的开始(?:7|0\d|\+94\d)
- 7
, or 0
and a digit or +94
and a digit (?:7|0\d|\+94\d)
- 7
或0
和一个数字或+94
和一个数字\d{8}
- eight digits \d{8}
- 八位数字)$
- end of the group, end of string. )$
- 组结束,字符串结束。See the demo below:请参阅下面的演示:
input:valid { color: navy } input:invalid { color: red; }
<form> <input type="text" name="mobile" class="form-control" pattern="(?:7|0\d|\+94\d)\d{8}" required /> <input type="Submit"/> </form>
Can try this hope it helps可以试试这个希望它有帮助
input:not(:placeholder-shown):invalid{ background-color:pink; box-shadow:0 0 0 2px red; }
<input type="text" name="mobile" class="form-control" placeholder="Phone number" pattern ="(7[0-9]{8}|0[0-9]{9}|\+94[0-9]{9})$" required />
If you are looking for a regex pattern that only works for your examples try this:如果您正在寻找仅适用于您的示例的正则表达式模式,请尝试以下操作:
(?:(?:7|0[1-9]{1})|(?:\+94))[0-9]{8}
7|0[1-9]{1}
accepts 7 or 0 and one other digit between 1-9 7|0[1-9]{1}
接受 7 或 0 以及 1-9 之间的另一个数字|
的|
is used to represent the "OR" condition用于表示“或”条件\+94
accepts +94 as the 3 beginning digits \+94
接受 +94 作为 3 个起始数字[0-9]{8}
means 8 other digits between 0 and 9 [0-9]{8}
表示 0 到 9 之间的其他 8 个数字If you are looking for a pattern that could work for a wide range of phone number patterns, you could try this:如果您正在寻找一种适用于各种电话号码模式的模式,您可以试试这个:
^[+]?[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$
How this works:这是如何工作的:
^
represents the beginning of the string ^
代表字符串的开头[+]?
for phone numbers that include "+" at the beginning对于开头包含“+”的电话号码[(]{0,1}
represents the opening " ( ". For phone numbers that include (###). Example: (222)333-333 [(]{0,1}
表示开头的“(”。对于包含(###)的电话号码。示例:(222)333-333[0-9]{1,4}
represent the number in between the "(###)" [0-9]{1,4}
代表“(###)”之间的数字[)]{0,1}
represent the closing ")" [)]{0,1}
代表结尾的“)”[-\s\./0-9]*
this will allow phone numbers to be accepted even if the numbers are divided using "-" or " " or "." [-\s\./0-9]*
这将允许电话号码被接受,即使号码使用“-”或“”或“.”分开。$
represents the end of the string $
代表字符串结束Check this site: https://regexr.com/检查这个网站: https://regexr.com/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.