验证电子邮件格式和独特性对DB [英] Validate email format and uniqueness against DB

查看:165
本文介绍了验证电子邮件格式和独特性对DB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个JSF表单,我有一个电子邮件字段。我需要验证的电子邮件格式,并检查了DB的独特性。

I am creating a JSF form where I have an email field. I need to validate the email format and check for uniqueness against DB.

我需要检查这个当终端用户已经进入电子邮件领域。如果它是present在数据库中,那么我们就需要到外地颜色更改为红色,否则为绿色。我想通过AJAX执行此。

I need to check this when the enduser has entered the email field. If it's present in the DB, then we need to change the field color to red and otherwise to green. I would like to perform this by ajax.

我已经看到了PHP的例子,但它不是我清楚怎么做,在JSF。

I have seen examples in PHP, but it's not clear to me how to do it in JSF.

推荐答案

对于一般的邮件格式验证,你可以使用<一个href="http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/f/validateRegex.html"><$c$c><f:validateRegex>.

For general email format validation, you could use <f:validateRegex>.

<h:inputText id="email" value="#{bean.email}">
    <f:validateRegex pattern="([^.@]+)(\.[^.@]+)*@([^.@]+\.)+([^.@]+)" />
</h:inputText>
<h:message for="email" />

要在模糊执行验证,添加<一href="http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/f/ajax.html"><$c$c><f:ajax事件=模糊&GT;

To perform validation on blur, add <f:ajax event="blur">.

<h:inputText id="email" value="#{bean.email}">
    <f:validateRegex pattern="([^.@]+)(\.[^.@]+)*@([^.@]+\.)+([^.@]+)" />
    <f:ajax event="blur" render="m_email" />
</h:inputText>
<h:message id="m_email" for="email" />

有关唯一的电子邮件验证,实现JSF的<一个href="http://docs.oracle.com/javaee/6/api/javax/faces/validator/Validator.html"><$c$c>Validator根据其合同的界面。

For unique email validation, implement the JSF Validator interface according its contract.

@FacesValidator("uniqueEmailValidator")
public class UniqueEmailValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        if (value == null) {
            return; // Let required="true" handle, if any.
        }

        String email = (String) value;

        if (yourUserService.existEmail(email)) {
            throw new ValidatorException(new FacesMessage(
                FacesMessage.SEVERITY_ERROR, "Email is already in use.", null));
        }
    }

}

和<一个注册它href="http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/f/validator.html"><$c$c><f:validator>

<h:inputText id="email" value="#{bean.email}">
    <f:validateRegex pattern="([^.@]+)(\.[^.@]+)*@([^.@]+\.)+([^.@]+)" />
    <f:validator validatorId="uniqueEmailValidator" />
    <f:ajax event="blur" render="m_email" />
</h:inputText>
<h:message id="m_email" for="email" />

要更改组件的样式,你可以在使用EL 风格的styleClass 属性。

To change component's style, you could use EL in style or styleClass attribute.

<h:inputText id="email" value="#{bean.email}" styleClass="#{component.valid ? (facesContext.postback ? 'ok' : '') : 'error'}">
    <f:validateRegex pattern="([^.@]+)(\.[^.@]+)*@([^.@]+\.)+([^.@]+)" />
    <f:validator validatorId="uniqueEmailValidator" />
    <f:ajax event="blur" render="@this m_email" />
</h:inputText>
<h:message id="m_email" for="email" />

您不一定需要jQuery的这一点。这也将一直不太可靠的jQuery的运行在客户端,而不是服务器端(你知道,最终用户有100%的控制权是什么在客户端上运行)。

You do not necessarily need jQuery for this. It would also have been less robust as jQuery runs at the client side, not the server side (you know, endusers have 100% control over what runs at the client side).

这篇关于验证电子邮件格式和独特性对DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆