☰ See All Chapters |
JSF <f:validateRegex> Tag
<f:validateRegex> tag is used to validate the input against regex (regular expression) pattern. This tag has below one attribute.
pattern: used to set the regex pattern.
<h:outputText value="Enter your mobile" />
<h:inputText id="ph" value="#{helloBean.phone}" required="true"
requiredMessage="Please enter phone number"
validatorMessage="Please enter 10 digit phone number">
<f:validateRegex pattern="\d{10}" />
</h:inputText>
<h:message for="ph" 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 phone;
public String getPhone() { return phone; }
public void setPhone(String phone) { this.phone = phone; }
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 mobile" /> <h:inputText id="ph" value="#{helloBean.phone}" required="true" requiredMessage="Please enter phone number" validatorMessage="Please enter 10 digit phone number"> <f:validateRegex pattern="\d{10}" /> </h:inputText> <h:message for="ph" 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"> Phone is <h:outputText value="#{helloBean.phone}" /> </h:panelGrid> </h1> </h:body> </html> |
Eclipse project directory structure
Output
All Chapters