×
☰ See All Chapters

JSF <f:validateLongRange> Tag

<f:validateLongRange> tag is used to validate the range of the Integer input. This tag has below two attributes.

  1. minimum: used to validate for minimum integer value 

  2. maximum: used to validate for maximum integer value 

<h:outputText value="Enter your ID" />

<h:inputText id="uid" value="#{helloBean.userId}"

        validatorMessage="UserId should be between 100 and 1000"

        converterMessage="Please enter integer value">

        <f:validateLongRange minimum="100" maximum="1000" />

</h:inputText>

<h:message for="uid" 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 int userId;

 

        public int getUserId() {

                return userId;

        }

 

        public void setUserId(int userId) {

                this.userId = userId;

        }

 

        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 ID" />

                        <h:inputText id="uid" value="#{helloBean.userId}"

                                validatorMessage="UserId should be between 100 and 1000"

                                converterMessage="Please enter integer value">

                                <f:validateLongRange minimum="100" maximum="1000" />

                        </h:inputText>

                        <h:message for="uid" 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">

                        User ID is

                        <h:outputText value="#{helloBean.userId}" />

                </h:panelGrid>

        </h1>

</h:body>

</html>

Eclipse project directory structure

jsf-validatelongrange-0
 

Output

jsf-validatelongrange-1
 
jsf-validatelongrange-2
 
jsf-validatelongrange-3
 

All Chapters
Author