☰ See All Chapters |
JSF <f:validateLength> Tag
<f:validateLength> tag is used to validate the minimum and maximum length of the String input. This tag has below two attributes.
minimum: used to validate for minimum length
maximum: used to validate for maximum length
<h:outputText value="Enter your name" />
<h:inputText id="nme" value="#{helloBean.name}"
validatorMessage="Length should be between 3 and 20">
<f:validateLength minimum="3" maximum="20" />
</h:inputText>
<h:message for="nme" style="color:red" />
JSF <f:validateLength> Tag Example
pom.xml
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.java4coding</groupId> <artifactId>JSF_Validator</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.2.6</version> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-impl</artifactId> <version>2.2.6</version> </dependency> </dependencies> <build> <finalName>JSF_Validator</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.1</version> <configuration> <source>1.9</source> <target>1.9</target> </configuration> </plugin> </plugins> </build> </project> |
web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "https://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> </web-app> |
HelloBean.java
package com.java4coding;
import javax.faces.bean.ManagedBean;
@ManagedBean public class HelloBean {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String display() { return "second"; }
} |
first.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml" xmlns:h="https://xmlns.jcp.org/jsf/html" xmlns:f="https://xmlns.jcp.org/jsf/core">
<h:head></h:head> <h:body> <h:form> <h:panelGrid columns="4"> <h:outputText value="Enter your name" /> <h:inputText id="nme" value="#{helloBean.name}" validatorMessage="Length should be between 3 and 20"> <f:validateLength minimum="3" maximum="20" /> </h:inputText> <h:message for="nme" style="color:red" /> </h:panelGrid>
<h:commandButton value="LOGIN" action="#{helloBean.display}" /> </h:form> </h:body> </html> |
second.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml" xmlns:h="https://xmlns.jcp.org/jsf/html" xmlns:f="https://xmlns.jcp.org/jsf/core">
<h:head></h:head> <h:body> <h1> <h:panelGrid columns="2"> Name is <h:outputText value="#{helloBean.name}" /> </h:panelGrid> </h1> </h:body> </html> |
Eclipse project directory structure
Output
All Chapters