电话号码的 HTML 验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37936138/
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
HTML Validation for phone numbers
提问by Jayashree venugopal
I am new to HTML,can any one please help me to validate a phone number only with '+' and numeric values and phone number shud not exceed 13 numbers,and to validate email with a '@' and a '.',I dont want to use javascript
我是 HTML 新手,有人可以帮助我仅使用“+”和数值验证电话号码,并且电话号码不能超过 13 个数字,并使用“@”和“.”验证电子邮件,我不想使用javascript
回答by John3136
In HTML5 you can use <input type='tel'>
and <input type='email'>
在 HTML5 中,您可以使用<input type='tel'>
和<input type='email'>
You can also specify a specific pattern like <input type='tel' pattern='[\+]\d{2}[\(]\d{2}[\)]\d{4}[\-]\d{4}' title='Phone Number (Format: +99(99)9999-9999)'>
您还可以指定一个特定的模式,如 <input type='tel' pattern='[\+]\d{2}[\(]\d{2}[\)]\d{4}[\-]\d{4}' title='Phone Number (Format: +99(99)9999-9999)'>
Something like pattern='^\+?\d{0,13}'
Would give you an optional + and up to 13 digits
类似的东西pattern='^\+?\d{0,13}'
会给你一个可选的 + 和最多 13 位数字
回答by Clement Amarnath
Email Validation - <input type="email" />
, use the type as email.
电子邮件验证 - <input type="email" />
,使用类型作为电子邮件。
Telephone number validation - <input type="tel" />
. use the type as tel.
电话号码验证 - <input type="tel" />
. 使用类型作为电话。
Reference:
参考:
https://www.sitepoint.com/client-side-form-validation-html5/
https://www.sitepoint.com/client-side-form-validation-html5/
回答by Nikita Yo LAHOLA
HTML and/or JavaScript will only validate email on client side, the more secure solution would be to validate email on the server side. Having said that you can do basic validation in HTML 5 like
HTML 和/或 JavaScript 只会在客户端验证电子邮件,更安全的解决方案是在服务器端验证电子邮件。话虽如此,您可以在 HTML 5 中进行基本验证,例如
<form>
<input type="email" placeholder="Your email" required>
<input type="submit" value="Submit">
</form>