Html 带模式的 HTML5 电话号码验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19611599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML5 phone number validation with pattern
提问by Manwal
I'm using HTML5 form validation to validate phone numbers from India.
我正在使用 HTML5 表单验证来验证来自印度的电话号码。
Phone numbers from India are 10 digits long, and start with 7, 8, or 9.
来自印度的电话号码有 10 位数字,以 7、8 或 9 开头。
For example:
例如:
- 7878787878
- 9898989898
- 8678678878
- 7878787878
- 9898989898
- 8678678878
These phone numbers are valid, but
这些电话号码是有效的,但
- 1212121212
- 3434545464
- 6545432322
- 1212121212
- 3434545464
- 6545432322
are invalid.
无效。
Suggest a pattern that can detect valid India phone numbers.
建议一种可以检测有效印度电话号码的模式。
So far, my pattern is [0-9]{10}
, but it doesn't check if the first digit is 7, 8, or 9.
到目前为止,我的模式是[0-9]{10}
,但它不检查第一个数字是 7、8 还是 9。
采纳答案by itsmikem
How about this? /(7|8|9)\d{9}/
这个怎么样? /(7|8|9)\d{9}/
It starts by either looking for 7 or 8 or 9, and then followed by 9 digits.
它首先查找 7 或 8 或 9,然后是 9 位数字。
回答by JamesPlayer
How about
怎么样
<input type="text" pattern="[789][0-9]{9}">
回答by Shailendr singh
The regex validation for india should make sure that +91 is used, then make sure that 7, 8,9 is used after +91 and finally followed by 9 digits.
印度的正则表达式验证应确保使用 +91,然后确保在 +91 之后使用 7, 8,9,最后是 9 位数字。
/^+91(7\d|8\d|9\d)\d{9}$/
/^+91(7\d|8\d|9\d)\d{9}$/
Your original regex doesn't require a "+" at the front though.
不过,您的原始正则表达式不需要前面的“+”。
Get the more information from below link
w3schools.com/jsref/jsref_obj_regexp.asp
Get the more information from below link
w3schools.com/jsref/jsref_obj_regexp.asp
回答by thejustv
Try this code:
试试这个代码:
<input type="text" name="Phone Number" pattern="[7-9]{1}[0-9]{9}"
title="Phone number with 7-9 and remaing 9 digit with 0-9">
This code will inputs only in the following format:
此代码将仅以以下格式输入:
9238726384 (starting with 9 or 8 or 7 and other 9 digit using any number)
8237373746
7383673874
9238726384(以 9 或 8 或 7 和其他 9 位数字开头)
8237373746
7383673874
Incorrect format:
2937389471(starting not with 9 or 8 or 7)
32796432796(more than 10 digit)
921543(less than 10 digit)
格式不正确:
2937389471(不是以 9 或 8 或 7
开头)32796432796(大于 10 位)
921543(小于 10 位)
回答by manish1706
This code will accept all country code with + sign
此代码将接受所有带 + 号的国家代码
<input type="text" pattern="[0-9]{5}[-][0-9]{7}[-][0-9]{1}"/>
Some countries allow a single "0" character instead of "+" and others a double "0" character instead of the "+". Neither are standard.
某些国家/地区允许使用单个“0”字符代替“+”,而其他国家/地区允许使用双“0”字符代替“+”。两者都不是标准的。