Is there a Java implementation of the HTML5 input email validation?

半城伤御伤魂 提交于 2019-12-04 03:17:29

You can use a regex:

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!