f:validateRegex” is a new validator tag in JSF 2.0, which is used to validate JSF component with a given regular expression pattern. For example,

<h:inputSecret id="password" value="#{user.password}">
  <f:validateRegex pattern="((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})" />

The above regex pattern is required 6 to 20 characters string with at least one digit, one upper case letter, one lower case letter and one special symbol (“@#$%”). This is strong and complex enough for a password validation, see this password validation with regular expression.

“f:validateRegex” example

A JSF 2.0 example to show the use of “f:validateRegex” tag to create a strong password validator.

1. Managed Bean

An user managed bean.

package com.mkyong;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
public class UserBean implements Serializable{
	String password;

	public String getPassword() {
		return password;

	public void setPassword(String password) {
		this.password = password;

2. JSF Page

JSF XHTML page, show the use of “f:validateRegex” tag to make sure the “password” field is match the given regular expression pattern.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns="http://www.w3.org/1999/xhtml"   
    <h1>JSF 2 validateRegex example</h1>
       <h:panelGrid columns="3">
	  Enter your password : 
	  <h:inputSecret id="password" value="#{user.password}" 
		size="20" required="true"
                   pattern="((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})" />
	  <h:message for="password" style="color:red" />
       <h:commandButton value="Submit" action="result" />

3. Demo

If “password” is not match the regex pattern, display the error message.


Download Source Code

Download It – JSF-2-ValidateRegex-Example.zip (9KB)


  1. JSF 2 validateRegex JavaDoc
  2. Password validation with regular expression