Table of contents
1.
Introduction
2.
JSF
3.
JSF f: validateLength
3.1.
Syntax
4.
Tag Attribute Used 
5.
Sample Code
6.
Data.java
6.1.
index.xhtml
6.2.
end.xhtml
7.
Output
8.
Frequently Asked Questions
8.1.
What are tags?
8.2.
What are controllers in JSF Application?
8.3.
What is servlet?
8.4.
What are APIs?
8.5.
What is XHTML?
9.
Conclusion
Last Updated: Mar 27, 2024

JSF f:validateLength

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

Introduction

Hey Ninjas!!

While working on a piece of code, the most encountered error is a validation error. Then how to check whether the string we enter is in the specified range or not. For this, in JSF, there is the f:validateLength tag which verifies whether the string is the specific given range or not.

So let's begin our journey to explore more about this topic.  

 

 

First, let's have a look at what JSF is.

JSF

The Java standard technology for creating component-based, event-oriented web interfaces is called JavaServer Faces (JSF). JSF permits server-side data and functionality access, just like JavaServer Pages (JSP). JSF is an XML document representing formal components in a logical tree, unlike JSP, which is just an HTML page with server-side capabilities. JSF components are supported by Java objects, separate from HTML, and possess all of Java's features, including the ability to access distant APIs and databases

Now let's have a look at the JSF f: validateLength

JSF f: validateLength

f:validateLength tag is used to verify the length of a string value inside a specific range.

Syntax

<f:validateLength minimum="a" maximum="b" />

Tag Attribute Used 

1. Minimum: It specifies the component's minimum length.

2.Maximum: It is used to specify the component's maximum length.

Sample Code

Data.java

package com. coding.ninjas;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "Data", eager = true)
@SessionScoped
public class UserData implements Serializable {
  private static final long serialVersionUID = 1L;
  private String name;
 
  public String getName() {
     return name;
  }
 
  public void setName(String name) {
     this.name = name;
  }  
}

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">
 
  <h:head>
     <title>localhost:8080l</title>
  </h:head>
 
  <h:body>
     <h2>h:validateLength Example</h2>
     
     <h:form>
        <h:inputText id = "nameInput" value = "#{Data.name}"
           label = "name" >
           <f:validateLength minimum = "5" maximum = "9" />
        </h:inputText>
        <h:commandButton value = "submit" action = "result"/>
        <h:message for = "nameInput" style = "color:red" />
     </h:form>  
 
  </h:body>
</html>

end.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:f = "http://java.sun.com/jsf/core"    
   xmlns:h = "http://java.sun.com/jsf/html">
   
   <h:head>
      <title>localhost:8080</title>   
   </h:head>
   
   <h:body>
      <h2>Result</h2>
      <hr />
      Name: #{Data.name}     
   </h:body>
</html>

 

Output

Frequently Asked Questions

What are tags?

HTML tags function similarly to keywords in that they specify how a web browser will format and present text. A web browser can distinguish between HTML material and plain content with the use of tags.

What are controllers in JSF Application?

"FacesServlet" is the central controller for the JSF application.

What is servlet?

A Java programming language class known as a servlet is used to increase the functionality of servers that host applications that are accessed via the request-response programming model.

What are APIs?

API stands for  Application Programming Interface , is a software bridge that enables communication between two applications.

What is XHTML?

EXtensible HyperText Markup Language (XHTML).To make HTML more adaptable and extendable so that it could interact with other data types, XHTML was created (such as XML).

Conclusion

Finally, you've concluded this article. 

Congratulations!! You learned about JSF - f: validateLength in this blog. You studied the tag and how it helps in validating the string length, attribute used, and a sample program.

After reading these interview questions, are you eager to read more articles on the subject of JSF? Don't worry; Coding Ninjas will take care of everything. Check out the awesome content on the Coding Ninjas Website

JSF 

JSF architecture

JSF Application Events

Please see our Code studio guided routes to learn more about DSA, Competitive Programming, JavaScript, System Design, and other topics. Also, enroll in our courses and use the accessible sample tests and problems. For placement preparations, have a look at the interview experiences and interview bundle.

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass