JSF Validators

Jsf is provided with in built validation tags to validate UI Component data.

Following are the validators tags in Jsf:

f:validateLength:Validates length of a string.

f:validateLongRange:Validates range of numeric value.

f:validateDoubleRange:Validates range of float value.

f:validateRegex:Validate JSF component with a given regular expression.

Custom Validator:Creating a custom validator.

Example:

index.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:body>

<h3>JSF Validators example</h3>

<h:form>
    Enter Name: 
<h:inputText id="name" value="#{user.name}" size="20"
label="name">
<f:validateLength minimum="5" maximum="8" />
</h:inputText>
<h:message for="name" style="color:red" />
<br/>
Enter Place: 
<h:inputText id="place" value="#{user.place}" size="20"
label="place" >
<f:validateRequired />
</h:inputText>
<h:message for="place" style="color:red" />
<br/>
 
Enter Age:
<h:inputText id="age" value="#{user.age}" 
         label="age" >
         <f:validateLongRange minimum="30" maximum="100" />
      </h:inputText>
      <h:message for="age" style="color:red" />
      <br/>
      Enter Salary:
      <h:inputText id="salary" value="#{user.salary}" 
         label="salary" >
         <f:validateDoubleRange minimum="50000.00" maximum="1000000.00" />
      </h:inputText>
      <h:message for="salary" style="color:red" />
      <br/>
      Enter password:
<h:inputSecret id="password" value="#{user.password}" 
         label="password" >
    <f:validateRegex pattern="((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})" />
      </h:inputSecret>
<h:message for="password" style="color:red" />
<br/>
<h:commandButton value="Submit" action="output" />
</h:form>

</h:body>
</html>

User Bean

package in.gov.tds.bean;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
 
@ManagedBean(name="user")
@SessionScoped
public class User implements Serializable{
 
/**
* 
*/
private static final long serialVersionUID = 1L;
String name;
int age;
double salary;
String password;
String place;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the salary
*/
public double getSalary() {
return salary;
}
/**
* @param salary the salary to set
*/
public void setSalary(double salary) {
this.salary = salary;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the place
*/
public String getPlace() {
return place;
}
/**
* @param place the place to set
*/
public void setPlace(String place) {
this.place = place;
}
  
}

OutputPage.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:body>

<h3>JSF Validators Example</h3>
 
Name :  
<h:outputText value="#{user.name}">
</h:outputText>
<br />
Place :  
<h:outputText value="#{user.place}">
</h:outputText>
<br />
Age :  
<h:outputText value="#{user.age}">
</h:outputText>
<br />
Salary :  
<h:outputText value="#{user.salary}">
</h:outputText>
<br />
Password :  
<h:outputText value="#{user.password}">
</h:outputText>

</h:body>
</html>

Run Application:



Enter Valid values and output will be: