Step1:

First create dynamic web project in Eclipse, add jar files of Mojarra or Apache myfaces.. (ur choice)..

Step2:

Add new package in src folder:
Package: com.test
Now create new class:
Name: EmailValidator.java


Step3:

Now  add this code in EmailValidator Class:
Code it:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
@FacesValidator("emailValidator")

public class EmailValidator implements Validator{

         private static final String EMAIL_PATTERN ="^[_A-Za-z0-9-]+(\\." +"[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*" + "(\\.[A-Za-z]{2,})$";
 private Pattern pattern;
private Matcher matcher;
   public EmailValidator(){
                    pattern = Pattern.compile(EMAIL_PATTERN);
      }
@Override
public void validate(FacesContext context, UIComponent component,
                             Object value) throws ValidatorException {
          matcher = pattern.matcher(value.toString());
          if(!matcher.matches()){
     FacesMessage msg =  new FacesMessage("E-mail validation failed.", "Invalid E-mail format.");
                            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                            throw new ValidatorException(msg);
              }
         }
}


Step 4:
Now Create EmailBean.java Class:
Name: EmailBean.java
Code it:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="test")
@SessionScoped
public class EmailBean {
private String email;
public void setEmail(String email) {
         this.email = email;
}
public String getEmail() {
         return email;
}
}


Step 5:
Now test custom validator, create .xhtml page:
Name: test.xhtml
Code it:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<h:form>
<h1>Hello .. It is testing Page</h1>
<br/>
<h:panelGrid>
Type Email:<h:inputText id="email" value="#{test.email}" size="20" required="true" label="Email Address">
<f:validator validatorId="emailValidator"/>
</h:inputText>
<h:message for="email"  />
</h:panelGrid>
<h:commandButton value="Submit" action="emailtest" />
</h:form>
</body>
</html>

Step 6:
Now create emailtest.xhtml page for getting result:
Code it:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
 <h:panelGrid columns="2">
 Email Address : 
 <h:outputText value="#{test.email}" />
 </h:panelGrid>
</body>
</html>
                                                                  Now Use this tutorial and enjoy your own validator..! :)

 
Start blogging by creating a new post. You can edit or delete me by clicking under the comments. You can also customize your sidebar by dragging in elements from the top bar.