import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
public class EmailValidator implements Serializable, Validator {
public EmailValidator() {
super();
}
public void validate(FacesContext facesContext, UIComponent uiComponent,
Object object) {
String value = (String)object;
if ((value == null) || value.length() == 0) {
System.out.println("null value found");
return;
//System.out.println("null value found");
}
//String expression="^[\\\\w\\\\-]([\\\\.\\\\w])+[\\\\w]+@([\\\\w\\\\-]+\\\\.)+[A-Z]{2,4}$";
String expression =
"^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
CharSequence inputstr = value;
Pattern pattern =
Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputstr);
if (!matcher.matches()) {
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Enter the proper Email Id",
null));
}
}
}
....................................
Now go to faces-config.xml and open validators tab give a meaningfull Id like EmailValidator and classname
in this case it should be com.rk.validator.EmailValidator
and the input file just write
<af:inputText label="Enter EmailID" id="it1" autoSubmit="true">
<f:validator validatorid="EmailValidator"/>
</af:inputText>
And you are done.