Srikanth Technologies

Creating Custom Validator In JSF

JSF comes with a few validators to validate the data entered into controls. When we have to do some custom validation, such as validation of pincode or vehicle number etc., we need to create our own validator using fairly a simple process.

Follow the given steps below to create a custom validator in JSF to validate pincode. The entered value must be starting with 53 and then contain 4 digits (ex.530016). Anything other than that is considered to be invalid.

Create Custom Validator

Creating a custom validator is all about creating a class that implements Validator interface. Validator interface contains a single method - validate(). Third parameter of this method contains the data that is to be validated. Here is the code for
package validators;

import java.util.regex.Pattern;
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 PincodeValidator implements Validator {

    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        // check whether value is starting with 53 and contains 4 digits after that.
        if (! Pattern.matches("^53\\d{4}$", value.toString()))
            throw new ValidatorException( new FacesMessage("Invalid Pincode!",null));   // error, so throw exception

Register Custom Validator

Register custom validator using faces-config.xml as follows.
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE faces-config PUBLIC
  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
<!-- =========== FULL CONFIGURATION FILE ================================== -->
    <!-- other entries -->
         <description>Validator to validate Pincode </description>

Validator-id is used to refer to validator from JSF form. Validator-class is the name of the class that implements Validator interface.

Using Custom Validator

Now it is time to validate pincode entered by user into textbox using custom validator. The following is a form that uses custom validator - PincodeValidator.

Create testpincodevalidator.jsp as follows to test PincodeValidator custom validator.

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

<%@taglib prefix="f" uri=""%>
<%@taglib prefix="h" uri=""%>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Testing PincodeValidator</title>
    <h3>Testing Custom Validator</h3>
            <h:form id="form1">
                Enter Pincode :
                <h:inputText  id="pincode" required="true" >
                    <f:validator validatorId="pincodevalidator" />
                <h:message id="errmsg" for="pincode" />
                <h:commandButton value="Submit" type="submit" />

Run testpincodevalidator.jsp and enter a value. If value is not according to the required pattern (53 followed by 4 digits) then an error message - Invalid Pincode! - is displayed on the right of textbox.