1 Reply Latest reply on Feb 12, 2013 1:40 AM by sivaprasad9394

    Validate Email Existance

    wingman

      Hi i want to validate the email on server side, how to validate whether the email present in the db, if present show error message.

      and i want to accomplish this before submitting submit button.

       

      Here is my sample page...........

       

      <div id="jskLogin">

      <rich:panel>

      <h:form id="fom1">

          <h:panelGrid columns="3">

              <h:outputText value="Email:"/>

              <h:inputText  value="#{userBean.email}" id="email" required="true">

              <rich:validator/>

              </h:inputText>

              <h:message for="email"/>

              <h:outputText value="Password:"/>

              <h:inputText  value="#{userBean.password}" id="password">

              <rich:validator/>

              </h:inputText>

              <h:message for="password"/>

          </h:panelGrid>

          <h:commandButton value="Register" action="#{userBean.save}"/>

      </h:form>

      </rich:panel>

      </div>

       

       

      Managed Bean..............

       

       

      @ManagedBean(name = "userBean")

      @RequestScoped

      public class UserBean implements Cloneable{

       

                @Size(min = 3, max = 32, message = "UserName is required")

          private String userName;

       

                @Size(min = 3, max = 32, message = "Email is required")

          private String email;

       

      //getters/setters

       

       

      can any one post sample code............

       

      thanks in advance

        • 1. Re: Validate Email Existance
          sivaprasad9394

          Hi Boby,

           

          Using ajax u can validate it like below,

          <a4j:region>

                          <h:panelGrid columns="5" border="0"

                              columnClasses="fieldLabel,fieldColon,fieldData,fieldError,fieldErrorNone">

                              <h:outputText value="#{msg.username}" />

                              <h:outputText value=":" />

                             

                              <h:inputText id="emailField1" value="#urBean.Email}"

                                  required="true" requiredMessage="#{msg.requiredField}"  styleClass="cel"

                                  validatorMessage="#{msg.emailLengthMessage}"

                                  size="28"

                                  maxlength="15">

                                  <f:validateLength  minimum="#{msg.emailMin}" maximum="#{msg.emailMax}">                           

                                  </f:validateLength>                      

                              <a4j:support event="onblur" actionListener="#{urBean.emailActionListener}"

                                  reRender="p_emailField,eemailField" limitToList="true" />

                              </h:inputText>

                              <h:outputText value="*" id ="erroremailField"/>

                              <a4j:outputPanel id="p_emailField" >

                                  <h:message id="eemailField"  for="emailField1" />

                              </a4j:outputPanel>

                          </h:panelGrid>

          </a4j:region>

           

          public void emailActionListener(ActionEvent event) {

              log.info("Entering emailActionListener method()......");

              Login login = new Login();

              try

              {

                if (this.selectedUser != null)

                {

                  ********** here check ur email with database you have already email existing or not ****************

                  if (lst.size() > 0) {

                    FacesContext faceContext = FacesContext.getCurrentInstance(); //Instead of doing like this create some boolean variable with false and set as true inside this method

                    FacesMessage facesMessage = new FacesMessage();//if email already exist in the DB means and just render the <h:output text in the screen with color red.

                    facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR); // it shows like error in the screen.       

                    facesMessage.setDetail(this.Resources.getString("emailExist"));

                    facesMessage.setSummary(this.Resources.getString("emailExist"));

                    faceContext.addMessage("mainInclude:userAddAloneView:pageForm:emailField", facesMessage);

                  }

                }

           

              }

              catch (Exception ex)

              {

                ex.printStackTrace();

              }

            }

           

          If you are using richfaces 4 you can alter the code as u like using a4j:ajax and execute ...i hope it works.

           

          Thanks and Regards,

          Siva