在 JSF 2 / PrimeFaces 中使用正则表达式进行电子邮件验证
我有一个输入电子邮件地址的输入字段:
<h:inputText value="#{register.user.email}" required="true" />
如何使用 JSF 2 / PrimeFaces 中的正则表达式验证输入的值是否为有效的电子邮件地址?
I have an input field taking an email address:
<h:inputText value="#{register.user.email}" required="true" />
How can I validate the entered value as a valid email address using regex in JSF 2 / PrimeFaces?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
所有基于拉丁字符验证电子邮件格式的正则表达式尝试均损坏。他们不支持自 2010 年 5 月起可用的国际化域名。是的,您没看错,从那时起,域名中允许使用非拉丁字符,因此电子邮件地址中也允许使用非拉丁字符。
因此,有非常多的可能的字符需要验证。最好的办法就是保持简单。以下正则表达式仅根据
@
和.
字符的出现来验证电子邮件格式。同样,这只是验证一般电子邮件格式,而不是验证电子邮件本身是否合法。您仍然可以输入
[email protected]
作为地址并通过验证。没有任何一个正则表达式可以涵盖这一点。如果电子邮件地址的有效性如此重要,请将其与身份验证系统结合起来。只需向相关电子邮件地址发送带有回调链接的某种激活电子邮件,然后让用户通过电子邮件地址登录即可。All regular expression attempts to validate the email format based on Latin characters are broken. They do not support internationalized domain names which were available since May 2010. Yes, you read it right, non-Latin characters are since then allowed in domain names and thus also email addresses.
That are thus extremely a lot of possible characters to validate. Best is to just keep it simple. The following regex just validates the email format based on the occurrence of the
@
and.
characters.Again, this just validates the general email format, not whether the email itself is legit. One can still enter
[email protected]
as address and pass the validation. No one regex can cover that. If the validity of the email address is that important, combine it with an authentication system. Just send some kind of an activation email with a callback link to the email address in question and let the user login by email address.方法如下:
我自己使用它......
丹尼尔。
Here is how:
Using it myself...
Daniel.
这是我的版本,它运行良好:
我在这里制作了一个演示
Here's my version and it works well :
And i made a demo here
这个支持电子邮件中的 unicode 域名:
...并且这个仅在输入电子邮件时验证电子邮件(电子邮件不是表单中的必填字段):
This one supports unicode domain names in email:
... and this one validates email only when email is entered (email is not required field in form):