Jsf validateRegex example

JSF f validateRegex tag:

JSF f: validateRegex tag is used to validate JSF component with a given regular expression pattern.

Syntax:

<f:validateRegex pattern="regularExpressionPatern" />

JSF f validateRegex tag attribute:

Attribute Description
  1. pattern
Formatting pattern

Example:

test.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>JSF f validate regex example.</title>
	</h:head>
    <h:body>
    	<h2>JSF f validate regex example.</h2>
 
    	<h:form>
    	   <h:inputSecret  id="passwordId" label="Password: " 
    	     value="#{test.password}" required="true">
    	     <f:validateRegex pattern="((?=.*[a-z]).{5,})" />
    	   </h:inputSecret>
    	   <br/>
    	   <h:message for="passwordId" style="color:red" />
    	   <br/>
    	   <h:commandButton value="Submit" action="success"/>  
    	</h:form>
 
    </h:body>
 
</html>

Test.java

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
 
/**
 * Managed bean.
 * @author w3schools
 */
@ManagedBean(name="test")
@SessionScoped
public class Test {
	private String password;
 
	public String getPassword() {
		return password;
	}
 
	public void setPassword(String password) {
		this.password = password;
	}
 
}

welcome.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>JSF f validate regex example.</title>
	</h:head>
    <h:body>
    	<h2>JSF f validate regex example.</h2> 
    	<h3>Password: <h:outputText value="#{test.password}"/></h3> 	
    </h:body>
 
</html>

faces-config.xml

<?xml version="1.0" encoding="windows-1252"?>
<faces-config version="2.0" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xi="http://www.w3.org/2001/XInclude" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
 
</faces-config>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    	xmlns="http://java.sun.com/xml/ns/javaee" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 
    <servlet>
        <servlet-name>faces</servlet-name>
        <servlet-class>
         javax.faces.webapp.FacesServlet
	<servlet-mapping>
        <servlet-name>faces</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
 
</web-app>

URL:

http://localhost:7001/JSFExample35/faces/test.xhtml

Output:

JSF example35-1
 
Enter Value.
JSF example35-2
 
Click Submit button.
JSF example35-3
 
Download this example.