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..! :)

7/17/2012 04:07:56 pm

I completely recognize everything you have stated. Really, I browsed by means of your various other articles and I do believe that you are definitely correct. Greatest wishes with this particular internet site.

Reply
4/1/2013 01:55:42 am

I intended to create you a very little word to be able to say thanks a lot yet again for your personal exceptional knowledge you have provided on this site. It is quite unbelievably open-handed of people like you to make openly what exactly most of us would've advertised for an ebook to make some money for themselves, and in particular considering the fact that you might have tried it if you desired. The secrets in addition worked to be a great way to recognize that other people have a similar interest just like my personal own to realize a great deal more concerning this condition. I am certain there are a lot more pleasurable moments in the future for folks who scan your site.

Reply
Noor ul Huda
4/1/2013 03:16:33 am

Thanks for appreciation...

Reply
1/10/2019 11:17:42 am

iup90ip[

Reply



Leave a Reply.