Html 为什么 HTML5 表单验证允许没有点的电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20573488/
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
Why does HTML5 form-validation allow emails without a dot?
提问by WEFX
I'm writing a very simple mock-up to demonstrate some HTML5 form-validation. However, I noticed the email validation doesn't check for a dot in the address, nor does it check for characters following said dot.
我正在编写一个非常简单的模型来演示一些 HTML5 表单验证。但是,我注意到电子邮件验证不会检查地址中的点,也不会检查所述点后面的字符。
In other words, "john@doe" is considered valid, when it's clearly not a valid email address; "doe" isn't a domain.
换句话说,当“john@doe”显然不是有效的电子邮件地址时,它被认为是有效的;“doe”不是域。
This is how I'm coding my email field:
这就是我对电子邮件字段进行编码的方式:
<input type="email" required />
Is that not enough?
这还不够吗?
Check this fiddleto see what I mean.
检查这个小提琴,看看我的意思。
Note: I know how to accomplish this via a RegEx pattern instead. I'm just wondering how someone could get away with using the email type instead.
注意:我知道如何通过 RegEx 模式来完成此操作。我只是想知道有人如何使用电子邮件类型来代替。
采纳答案by Ali Alavi
Because a@b is a valid email address (eg localhost is a valid domain). See http://en.wikipedia.org/wiki/Email_address#Examples
因为 a@b 是有效的电子邮件地址(例如 localhost 是有效的域)。请参阅http://en.wikipedia.org/wiki/Email_address#Examples
Also, keep in mind that you should always do the input validation in server. The client side validation should be only for giving feedback to the user and not be relied on, since it can be easily bypassed.
另外,请记住,您应该始终在服务器中进行输入验证。客户端验证应该仅用于向用户提供反馈而不是依赖它,因为它很容易被绕过。
回答by DBS
You can theoretically have an address without a "." in.
理论上,您可以拥有一个没有“.”的地址。在。
Since technically things such as:
由于技术上的事情,例如:
user@com
user@localserver
user@[IPv6:2001:db8::1]
Are all valid emails.
都是有效的电子邮件。
So the standard HTML5 validation allows for all valid E-mails, including the uncommon ones.
所以标准的 HTML5 验证允许所有有效的电子邮件,包括不常见的。
For some easy to read explanations (Instead of reading through the standards): http://en.wikipedia.org/wiki/Email_address#Examples
对于一些易于阅读的解释(而不是通读标准):http: //en.wikipedia.org/wiki/Email_address#Examples
回答by APAD1
回答by Ortomala Lokni
The RFC 822, chapter 6, gives the specification of an address in augmented Backus-Naur Form (BNF):
的RFC 822,第6章,给出了一个地址的在扩充巴科斯范式(BNF)规范:
addr-spec = local-part "@" domain
local-part = word *("." word)
domain = sub-domain *("." sub-domain)
Using this specification a@b
is a valid address.
使用此规范a@b
是一个有效的地址。
UPDATE
更新
To answer the comment of Trejkaz, I add the following definitions. We see that SPACE are allowed but only in quoted string.
为了回答 Trejkaz 的评论,我添加了以下定义。我们看到 SPACE 是允许的,但只能在带引号的字符串中。
word = atom / quoted-string
atom = 1*<any CHAR except specials, SPACE and CTLs>
quoted-string = <"> *(qtext/quoted-pair) <">
SPACE = <ASCII SP, space>
CTL = <any ASCII control character and DEL>
qtext = <any CHAR excepting <">, "\" & CR, and including linear-white-space>
quoted-pair = "\" CHAR
回答by gitaarik
On this MDN page it shows the regex browsers should use to validate the email:
在这个 MDN 页面上,它显示了正则表达式浏览器应该用来验证电子邮件:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/email#Validation
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/email#Validation
You can slightly change this regex to require at least one dot in the domain name: change the star *
at the end of the regex to a plus +
. Then use that regex as the pattern
attribute:
您可以稍微更改此正则表达式以要求域名中至少有一个点:*
将正则表达式末尾的星号更改为加号+
。然后使用该正则表达式作为pattern
属性:
<input type="email" pattern="^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$"></input>
回答by Dorian
You can customize the pattern of the email field:
您可以自定义电子邮件字段的模式:
input:valid {
border-color: green
}
input:invalid {
border-color: red
}
Email:
<input type="email" required value="[email protected]" /><br>
Non-dots Email:
<input type="email" required pattern="[^.]+@[^.]+" value="[email protected]" />
回答by Hari Das
Here is how you can do it with html5 using regex pattern. You can also include a custom message to display.
以下是使用正则表达式模式使用 html5 执行此操作的方法。您还可以包含要显示的自定义消息。
<form>
<input type="email" value="paul@test" required pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,63}$" title="Hey, you are missing domain part in the email !!!"/>
<button type="submit">Click Me</button>
</form>
回答by TSlegaitis
This pattern always works for me.
这种模式总是对我有用。
Text must in lowercase pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$"
but I think it covers more or less most emails.
文本必须小写, pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$"
但我认为它或多或少涵盖了大多数电子邮件。