Main Tutorials

JSF 2 validateDoubleRange example

f:validateDoubleRange” is a JSF range validator tag, which is used to validate the range of a floating point value. For example,


<h:inputText id="salary" value="#{user.salary}">
	<f:validateDoubleRange minimum="10.11" maximum="10000.11" />
</h:inputText>				

When this form is submitted, the validator will make sure the “salary” value is within the range from “10.11” to “10000.11”.

“f:validateDoubleRange” example

A JSF 2.0 example to show the use of “f:validateDoubleRange” tag to validate the range of a “salary” input field, when the validator failed, display the error message via “h:message” tag.

1. Managed Bean

An user managed bean, with a “salary” property.


package com.mkyong;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
 
@ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable{
	
	double salary;

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}
	
}

2. JSF Page

JSF XHTML page, show the use of “f:validateDoubleRange” tag to make sure the “salary” input value is within the range from “10.11” to “10000.11”.


<?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"
      >
    <h:body>
    	
    	<h1>JSF 2 validateDoubleRange example</h1>
		
	<h:form>
		
		<h:panelGrid columns="3">
			
			Enter your salary : 
				
			<h:inputText id="salary" value="#{user.salary}" 
				size="10" required="true"
				label="Salary" >
					
				<f:validateDoubleRange minimum="10.11" maximum="10000.11" />
					
			</h:inputText>
				
			<h:message for="salary" style="color:red" />
			
		</h:panelGrid>
			
		<h:commandButton value="Submit" action="result" />
			
	</h:form>
		
    </h:body>
</html>

3. Demo

Minimum range validation failed.

jsf2-ValidateDoubleRange-Example-1

Download Source Code

Reference

  1. JSF 2 validateDoubleRange JavaDoc
  2. Wiki floating point explanation

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Raveendra
13 years ago

Hi Mkyong,

I have a validation problem in my project i need to put two decimal after point(.)

I am not able to get solution for this problem using can help in this

regards,