Home Jsf Jsftraining Using Standard Validator & Custom Validator
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Using Standard Validator & Custom Validator

Advertisement
JSF validation is used to make sure that the component is going to obtain the expected content.

Using Standard Validator & Custom Validator

        

JSF validation is used to make sure that the component is going to obtain the expected content, such as:

1. Length is between 1 and 10
2. Float is between 2.0 and 10.0
3. Email address is in correct format etc.

You can implement it using standard validators supplied by JSF or by creating your own custom validators.

 

Standard validation

JSF provides standard validators like:

DoubleRangeValidator Value must be numeric type and in range between minimum and/or maximum values.
LongRangeValidator Value must be numeric type, convertible to long and in range between minimum and/or maximum values.
LengthValidator Value must be string type and length must be in range between minimum and/or maximum values.

For Example:

1. <f:validateLength /> is used to validate whether the length of the value lies in specified range.

<h:inputText id="name" required="true">
   <f:validateLength maximum="10" minimum="1"/>
</h:inputText>

<h:message for="name" />

2. <f:validateDoubleRange/> is used to validate whether the double value lies in specified range.

<h:inputText id="no" required="true">
   <f:validateDoubleRange minimum="2.0" maximum="10.0"/>
</h:inputText>

<h:message for="no" />

3. <f:validateLongRange"/> is used to validate whether the long value lies in specified range.

<h:inputText id = "lno" required = "true">
  <f:validateLongRange minimum = "2" maximum = "10"/>
</h:inputText><br/>

<h:message for="lno" />

 

Custom validation

If validation for the component value is not supported by the standard validators then you will need to create your own validator. For example, you can create email validator to validate the email text entered by the user i.e. whether the email id is in correct format. Using custom validator you will be able to display your own validation error message.

Steps to create a custom validator:

1. Create a class that implements the javax.faces.validator.Validator interface

2. Implement the validate() method (defined in Validator interface).

package roseindia;

import javax.faces.*;
import javax.faces.validator.*;
import javax.faces.application.*;
import javax.faces.component.*;
import javax.faces.context.*;
import java.util.regex.*;

public class Validation implements Validator{
public Validation(){}

public void validate(FacesContext facesContext, UIComponent uIComponent, Object object) throws ValidatorException{
   String enteredEmail = (String)object;
   Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
   Matcher m = p.matcher(enteredEmail);
   boolean matchFound = m.matches();

   if (!matchFound) {
   FacesMessage message = new FacesMessage();
   message.setSummary("Invalid Email ID.");
   throw new ValidatorException(message);
   }
   }
}

3. Register this validator in the configuration file "faces-confix.xml".

<validator>
  <validator-id>checkvalidemail</validator-id>
  <validator-class>roseindia.Validation</validator-class>
</validator>

4. Use this validator by <f:validator/> tag in the JSP page.
5. Use <h:message> tag in the jsp page to display the error message.

<h:inputText id="email" required="true">
   <f:validator validatorId="checkvalidemail" />
</h:inputText>

<h:message for="email" />

 

Download code for all examples

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 30, 2008

Related Tutorials

Ask Questions?    Discuss: Using Standard Validator & Custom Validator  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com