Table of contents
1.
Introduction
2.
JSF
3.
validateLongRange 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

JSF validateLongRange 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 validateLongRange. 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.

validateLongRange Tag

It validates the input received from the component to be of the type "long". Only those inputs can be validated using this tag which can be converted into 'long'.

Syntax: <f:validateLongRange minimum = "123" maximum = "456" />

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

validateLongRange 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 age property when the form is submitted.

User-managed bean code

//Java code for sample JSF Program
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name="ninja")
@SessionScoped
public class CodingNinjas implements Serializable{
int age;
//getter function to fetch the age
public int getAge() {
return age;
}
//setter function to set the age
public void setAge(int age) {
this.age = age;
}
}
You can also try this code with Online Java Compiler
Run Code

JSF Page

The JSF XHTML page uses the "f:validateLongRange" tag to ensure the 'age' is between 1 and 150.

<?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 validateLongRange example</h1>
<h:form>
<h:panelGrid columns="3">
Enter your age : 
<h:inputText id="age" value="#{ninja.age}" 
size="10" required="true"
label="Age" >
<f:validateLongRange maximum="150" minimum="1" />
</h:inputText>
<h:message for="age" 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 long-range validation in JSF. You went through the tag, its attributes and a sample program.

After reading about long-range validation in JSF, 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