Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Using Standard Validator & Custom Validator 
 

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

                          

» View all related tutorials
Related Tags: c file ide orm text date jsf form internationalization time air button io dates format label numbers vi locale key

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.