Table of contents
1.
Introduction
2.
JSF                
3.
validateDoubleRange Tag
4.
Attributes
5.
Sample Program
6.
Frequently Asked Questions
6.1.
What is a tag?
6.2.
What is the user interface?
6.3.
What is a framework?
6.4.
What is the prerequisite to running the JSF program?
6.5.
What are import statements?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

JSF validateDoubleRange Tag

Author Manish Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hey Ninja!! Have you ever been in a situation where you had to test whether a statement is true or false? While developing websites, you will encounter problems where validation is required. JSF provides a built-in tag to verify different types of user input. In this blog, you will learn one such validation tag viz validateDoubleRange. Let's get started!!

Source: wikipedia.org

JSF                

JSF(Java Server Faces) is a server-side framework for developing user interfaces. It is written in Java, follows a component-based architectural style, and provides standard tools for managing these components. It contains powerful APIs and a large set of tag libraries.

The APIs provide components and manages their state. It facilitates server-side validation, extensibility, accessibility and data conversions. Tag libraries help set up elements on web pages and connect them with objects on the backend. It contains handlers that implement component tags.

validateDoubleRange Tag

It validates the input received from the component to be of the type "double". Only those inputs can be validated using this tag which can be converted into floating-point values.

Syntax: <f:validateDoubleRange minimum = "123.11" maximum = "456.22" />

The structure of this tag is similar to any HTML tag with a '<' opening angle bracket, then comes the tag name and attributes and then the final closing angle bracket. It is a self-closing tag.

f: attribute – It passes attribute values to a component or parameter via a listener.

Attributes

validateDoubleRange tag has two attributes, namely minimum and maximum.

minimum:- It defines the minimum value of the component variable.

maximum:-  It represents the maximum value of the component variable.

Sample Program

This program will validate the salary property when the form is submitted, and the specified range is 10.11 to 10000.11

 

User-managed bean code

//Java Code to implement JSF double range validation
package com.codingNinjas;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name="ninja")
@SessionScoped
//Class definition
public class CodingNinjas implements Serializable{
double salary;
//getter function to fetch salary
public double getSalary() {
return salary;
}
//setter function to set salary value
public void setSalary(double salary) {
this.salary = salary;
}
}

JSF Page

The JSF XHTML page uses the "f:validateDoubleRange" tag to ensure the ‘salary’ is between 10.11  and 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="#{ninja.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>

 Output: 

Frequently Asked Questions

What is a tag?

A tag is a syntactical way of implementing program logic. These tags are like keywords which define how to display elements on the web browser. For example <html>, <img> etc.

What is the user interface?

The user interface is the visual components rendered on screen. UI is the interface between humans and machines. Examples: Speedometers, VR, ATM screens etc.

What is a framework?

Ready-made tools are available to implement directly in the codebase is called a framework. It provides an abstraction for the developer by bringing together the most generic pieces of code.

What is the prerequisite to running the JSF program?

Java Runtime Environment, JDK, Maven and Apache tomcat facilitate the running of JSF applications.

What are import statements?

Import statement fetches pre-written libraries, which help in the faster development of applications. These libraries are provided by organisations or language developers.

Conclusion

Finally, you have made it to the end of this article. Congratulations!! In this blog, you learnt about double-range validation in JSF. You went through the tag, its attributes and a sample program.

After reading these interview questions, are you not feeling excited to read more articles on the topic of JSF? Don't worry; Coding Ninjas has you covered. To learn, see the JSF.

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enrol in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Please do upvote our blogs if you find them helpful and informative!

Happy learning!

Live masterclass