I think the following example will work for you: http://viralpatel.net/blogs/2009/02/javaserver-faces-jsf-validation-tutorial-error-handling-jsf-validator.html . It even has a email address validator as an example.
e.g., from that page:
Write a validator by implementing the Validator
interface:
package net.viralpatel.jsf.helloworld;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
public class EmailValidator implements Validator{
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
String email = (String) value;
if(!email.contains("@")) {
FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_ERROR);
message.setSummary("Email is not valid.");
message.setDetail("Email is not valid.");
context.addMessage("userForm:Email", message);
throw new ValidatorException(message);
}
}
}
Then, you need a reference to this validator in your faces-config.xml
<validator>
<validator-id>emailValidator</validator-id>
<validator-class>net.viralpatel.jsf.helloworld.EmailValidator</validator-class>
</validator>
Then, use it!
<h:inputText id="Email" value="#{userBean.email}" required="true">
<f:validator validatorId="emailValidator" />
</h:inputText>