JSF Validate RegEx Input Field Example

This tutorial shows how to use f:validateRegex JSF Validate Regex. This validator validates if an input field matches to the provide pattern attribute. The entire pattern is matched against the String value of the input field. If it matches, it’s valid.

<h:inputText id="email" value="#{userBean.email}">
    <f:validateRegex pattern="^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"/>

JSF Managed Bean

package com.memorynotfound.jsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

public class UserBean {

    private String email;

    public String getEmail() {
        return email;

    public void setEmail(String email) {
        this.email = email;


Validate Emtpy Input

If you are experiencing that JSF does not validate empty fields, then you should add the following snippet to your web.xml. Using this property tells JSF to validate empty fields.


JSF Validate Length Input Field Page

<?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"
    <h2>JSF validate regex example</h2>
        <h:panelGrid columns="2">
            <h:outputLabel value="Email:">
                <h:inputText id="email" value="#{userBean.email}">
                    <f:validateRegex pattern="^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"/>
            <h:message for="email" style="color:red" />
        <h:commandButton value="submit" action="result"/>


URL: http://localhost:8080/jsf-validate-regex/

JSF Validate Regex

jsf validate regex example



You may also like...