JSF Tutorial - JSF Custom Validation Error Message Example

The following code shows how to create custom validation message.


The following code is from UserBean.java.

package com.java2s.common;

import java.io.Serializable;
import java.sql.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
public class UserBean implements Serializable{
  String username;
  Date dob;

  public String getUsername() {
    return username;
  public void setUsername(String username) {
    this.username = username;
  public Date getDob() {
    return dob;
  public void setDob(Date dob) {
    this.dob = dob;


The following code is from demo.xhtml.

<?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"   
      <h:panelGrid columns="3">
        Enter your username :
        <h:inputText id="username" value="#{user.username}" 
          size="20" required="true" label="Username">
          <f:validateLength minimum="5" maximum="10" />
        <h:message for="username" style="color:red" />
        Enter your DOB :
        <h:inputText id="dob" value="#{user.dob}" 
          size="20" required="true" label="Date of Birth">
          <f:convertDateTime />
        <h:message for="dob" style="color:red" />
      <h:commandButton value="Submit" action="result" />

The following code is from MyMessage.properties.

javax.faces.converter.DateTimeConverter.DATE={2}: ''{0}'' could not be understood as a date.
javax.faces.converter.DateTimeConverter.DATE_detail=Invalid date format.

javax.faces.validator.LengthValidator.MINIMUM=Minimum length of ''{0}'' is required.

The following code is from result.xhtml.


Download Custom-Validation-Error-Message.zip


Copy the generated WAR file from the target folder to Tomcat deployment folder and run Tomcat-Install-folder/bin/startup.bat.

After Tomcat finish starting, type the following URL in the browser address bar.