Main Tutorials

JSF 2 validateRegex example

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})" />
</h:inputSecret>

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;
 
@ManagedBean(name="user")
@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" 
"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:body>
    	
    <h1>JSF 2 validateRegex example</h1>
		
     <h:form>
		
       <h:panelGrid columns="3">
			
	  Enter your password : 
				
	  <h:inputSecret id="password" value="#{user.password}" 
		size="20" required="true"
		label="Password">
		<f:validateRegex 
                   pattern="((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})" />
	  </h:inputSecret>
				
	  <h:message for="password" style="color:red" />
			
       </h:panelGrid>
			
       <h:commandButton value="Submit" action="result" />
			
     </h:form>	
    </h:body>
</html>

3. Demo

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

jsf2-ValidateRegex-Example

Download Source Code

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

Reference

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

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
5 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Gouri
5 years ago

How to escape double quotes?

Satyam
10 years ago

How to change the message on validation failed. To have some custom message like “Please enter in the format ###-###-####”

Jose
10 years ago
Reply to  Satyam

validatorMessage=”Email not valid”

Manju
11 years ago

Very nice and useful links.
Thanks a lot!!

leonel
12 years ago

how associate the validator to one specific commandButton?, because in this case the validation is done to all commandButtons