是否有 HTML5 输入电子邮件验证的 Java 实现?

发布于 2024-10-16 09:28:18 字数 1642 浏览 2 评论 0原文

我想使用新的 元素。我希望 Java 代码能够在服务器上实现与浏览器中发生的验证相同的验证。

HTML5 规范在 < 中定义了电子邮件地址 a href="https://www.rfc-editor.org/rfc/rfc5234" rel="nofollow noreferrer">ABNF 为:

1*( atext / "." ) "@" ldh-str *( "." ldh-str )

其中

<ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>

<let-dig-hyp> ::= <let-dig> | "-"

<let-dig> ::= <letter> | <digit>

<letter> ::= any one of the 52 alphabetic characters A through Z in upper case and a through z in lower case

<digit> ::= any one of the ten digits 0 through 9

atext           =   ALPHA / DIGIT /    ; Printable US-ASCII
                       "!" / "#" /        ;  characters not including
                       "$" / "%" /        ;  specials.  Used for atoms.
                       "&" / "'" /
                       "*" / "+" /
                       "-" / "/" /
                       "=" / "?" /
                       "^" / "_" /
                       "`" / "{" /
                       "|" / "}" /
                       "~"

这些规则 “nofollow noreferrer”>RFC 5322。 如何在 Java 中测试地址是否符合这些规则?

谢谢!

I'd like to use the new <input type="email" /> element. I'd like to have Java code that implements the same validation on the server that happens in the browser.

The HTML5 spec defines email addresses in ABNF as:

1*( atext / "." ) "@" ldh-str *( "." ldh-str )

where:

<ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>

<let-dig-hyp> ::= <let-dig> | "-"

<let-dig> ::= <letter> | <digit>

<letter> ::= any one of the 52 alphabetic characters A through Z in upper case and a through z in lower case

<digit> ::= any one of the ten digits 0 through 9

and:

atext           =   ALPHA / DIGIT /    ; Printable US-ASCII
                       "!" / "#" /        ;  characters not including
                       "
quot; / "%" /        ;  specials.  Used for atoms.
                       "&" / "'" /
                       "*" / "+" /
                       "-" / "/" /
                       "=" / "?" /
                       "^" / "_" /
                       "`" / "{" /
                       "|" / "}" /
                       "~"

These are not the same rules as in RFC 5322.
How can I test that an address complies with these rules in Java?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春花秋月 2024-10-23 09:28:18

您可以使用正则表达式:

[A-Za-z0-9!#$%&'*+-/=?^_`{|}~]+@[A-Za-z0-9-]+(.[A-Za-z0-9-]+)*

You can use a regex:

[A-Za-z0-9!#$%&'*+-/=?^_`{|}~]+@[A-Za-z0-9-]+(.[A-Za-z0-9-]+)*

邮友 2024-10-23 09:28:18

实际上,W3C 建议您'vecite 提供了一个正则表达式,相当于他们所呈现的 ABNF,它定义了一个有效的电子邮件地址:

/^[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])?)*$/

但是这个正则表达式匹配无效的电子邮件地址,例如“.any..address.@123”(用https://regex101.com/)。

此正则表达式接受(根据 Wikipedia,电子邮件地址中的所有内容均无效):

  • “。 ” 的开头。
  • (点)位于本地部分“.” (点)在局部部分末尾
  • 多个连续的“.”本地部分中的(点)
  • 仅域部分中的数字

并拒绝(根据维基百科有效):

  • Unicode 字符
  • 一些用引号分隔的特殊字符 (")

请注意,W3C指出他们提出的规范是故意违规RFC 5322 的 ,所以他们有一个“借口”放弃有效的情况,但恕我直言,这不是接受无效地址的理由。

如果您不想处理这些异常情况,则可以使用 W3C 建议的正则表达式,否则,您应该使用正则表达式来涵盖您想要处理的情况。 。

Actually, The W3C Recommendation you've cited offers a regex as the equivalent for what they present as the ABNF which defines a valid email address:

/^[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])?)*$/

But this regex matches invalid email addresses, such as ".any..address.@123" (tested with https://regex101.com/).

This regex accepts (all invalid in an email address, according to Wikipedia):

  • "." (dot) at the beginning of local part
  • "." (dot) at the end of local part
  • multiple sequential "." (dot) in the local part
  • only numbers in domain part

and rejects (valid according to Wikipedia):

  • Unicode characters
  • some special characters delimited with quotation marks (")

Notice that W3C states that the specification they present is a willful violation of RFC 5322, so they have an "excuse" to leave off the valid cases, but IMHO it's not a reason to accept invalid addresses.

If you won't bother with those exception cases, you can use the regex that W3C suggests. Otherwise, you should work the regex to cover the cases you want to handle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文