在 JSF 2 / PrimeFaces 中使用正则表达式进行电子邮件验证

发布于 2024-12-11 11:00:04 字数 179 浏览 0 评论 0原文

我有一个输入电子邮件地址的输入字段:

<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 技术交流群。

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

发布评论

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

评论(5

南薇 2024-12-18 11:00:05
<p:inputText id="email" required="true" label="email" size="40"
    requiredMessage="Please enter your email address."
    validatorMessage="Invalid email format"
    value="#{userBean.email}">

  <f:validateRegex
    pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$" />

</p:inputText>
<p:watermark for="email" value="Email Address *" />
<p:message for="email" />

<p:commandButton value="test" style="margin:20px"
    action="#{userBean.register}" ajax="false" />

<p:inputText id="email" required="true" label="email" size="40"
    requiredMessage="Please enter your email address."
    validatorMessage="Invalid email format"
    value="#{userBean.email}">

  <f:validateRegex
    pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$" />

</p:inputText>
<p:watermark for="email" value="Email Address *" />
<p:message for="email" />

<p:commandButton value="test" style="margin:20px"
    action="#{userBean.register}" ajax="false" />

愁杀 2024-12-18 11:00:04

所有基于拉丁字符验证电子邮件格式的正则表达式尝试均损坏。他们不支持自 2010 年 5 月起可用的国际化域名。是的,您没看错,从那时起,域名中允许使用非拉丁字符,因此电子邮件地址中也允许使用非拉丁字符。

因此,有非常多的可能的字符需要验证。最好的办法就是保持简单。以下正则表达式仅根据 @. 字符的出现来验证电子邮件格式。

<f:validateRegex pattern="([^.@]+)(\.[^.@]+)*@([^.@]+\.)+([^.@]+)" />

同样,这只是验证一般电子邮件格式,而不是验证电子邮件本身是否合法。您仍然可以输入 [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.

<f:validateRegex pattern="([^.@]+)(\.[^.@]+)*@([^.@]+\.)+([^.@]+)" />

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.

梦幻的味道 2024-12-18 11:00:04

方法如下:

我自己使用它......

<h:inputText id="email" value="#{settingsBean.aFriendEmail}" required="true" label="Email" validatorMessage="#{settingsBean.aFriendEmail} is not valid">
    <f:validateRegex pattern="[\w\.-]*[a-zA-Z0-9_]@[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]" />
</h:inputText>
<p:message for="email" />

丹尼尔。

Here is how:

Using it myself...

<h:inputText id="email" value="#{settingsBean.aFriendEmail}" required="true" label="Email" validatorMessage="#{settingsBean.aFriendEmail} is not valid">
    <f:validateRegex pattern="[\w\.-]*[a-zA-Z0-9_]@[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]" />
</h:inputText>
<p:message for="email" />

Daniel.

银河中√捞星星 2024-12-18 11:00:04

这是我的版本,它运行良好:

<f:validateRegex pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$" />

我在这里制作了一个演示

Here's my version and it works well :

<f:validateRegex pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$" />

And i made a demo here

妳是的陽光 2024-12-18 11:00:04

这个支持电子邮件中的 unicode 域名:

<f:validateRegex pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[\p{L}\p{M}\p{N}.-]*(\.[\p{L}\p{M}]{2,})$" />

...并且这个仅在输入电子邮件时验证电子邮件(电子邮件不是表单中的必填字段):

<f:validateRegex pattern="(^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[\p{L}\p{M}\p{N}.-]*(\.[\p{L}\p{M}]{2,})$)?" />

This one supports unicode domain names in email:

<f:validateRegex pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[\p{L}\p{M}\p{N}.-]*(\.[\p{L}\p{M}]{2,})$" />

... and this one validates email only when email is entered (email is not required field in form):

<f:validateRegex pattern="(^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[\p{L}\p{M}\p{N}.-]*(\.[\p{L}\p{M}]{2,})$)?" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文