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 PincodeValidator.java.
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"
  "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<!-- =========== FULL CONFIGURATION FILE ================================== -->
<faces-config>
    <!-- other entries -->
    <validator>
         <description>Validator to validate Pincode </description>
         <validator-id>pincodevalidator</validator-id>
         <validator-class>validators.PincodeValidator</validator-class>
    </validator>
</faces-config>


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"
   "http://www.w3.org/TR/html4/loose.dtd">

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

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


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.




Post Your Comment

Image

Enter the code given in the above image :
Enter Your Name :
Enter Your Email Address :
Enter Your Comment :

Comments


Posted By Cactus On 23-Sep-12 08:08:04 AM

A provcoatvie insight! Just what we need!