Steps to provide customized validation messages

Steps to provide customized validation messages

Step1 – Prepare a properties file with the custom validation msgs

There prepare a properties file under classes folder or under src folder, pass all the predefined validation msgs in the present properties file from messages. Properties file and edit the validation message as per our requirement

Eg: abc. Properties


Should not be less than{0}

Javax. faces. Validator. Long range Validator. maximum={1}value should not be greater than{0}


Step – 2

Configure properties file in faces-config.xml file

To configure validation messages properties file, we have to use the following tags in faces- config.xml file







Step – 3

Provide label attribute in the respective i/p components

The main intension of label attribute is to override the default component id-component name in validation messages with the provided value

<h:input text id=”uname” value=”#{reg Bean. Uname}” required=”true” label=”user name”/>





abc. properties

Javar. faces. Validator. NOT-IN-RANGE=value is not in the range from{0}to{1}.

Javax. faces. Validator. Double range Validator. NOT-IN-RANGE= {2}.value must be in between the range {0} and{1}

Javax. faces. validator. Double range validator. TYPE={0} must be Double

Javax. faces. validator. Long range validator. NOT-IN RANGE ={2} value must be in the  range {0} and{1}

Javax. faces. validator. Long range validator. TYPE={0} must be Double long

Javax. faces. Component . UIInput. REQUIRED={0} Is required





<meta http-equiy=”Refresh”

Content=”2:URL=http://local host:1010/standaral dationsapp/registration from.dss”>

<title>insert title here </title>

</ head >



<h1>application Loading….< /h1>




Registration form.jsp:

<%@page language=”java”conten type=”text/html;”>

<%@taglib prefix=”f”



<head >

<meta http-equiv=” Content type” Content= ”text/ html; chanset= ISO-8859-1”>

<TITLE>Insert title here</title>

</head >




<h:messages style=”color: blue”/>

<h:panel Grid columns=”3”>

<h: input text id=”u name” value=”#{reg bean. uname}” required=”true” label=”user name”/>

<h: messages for= ”uname” style=”color:red”/>

<h:output text value=”password”/>

<h:input secret id=”upwd”value=”#{ reg bean. upwd}”

required=”true” label=”password”/>

<f:validate length minimum=”6”maximum=”15”/>

</h:input secret>

<h: messages for= ”upwd” style=”color: red”/>

<h: output text value=”user age”/>

<h:input text id=”uaage” value=”#{ reg bean. uage}”

required=”true” label=”user age”>

<f:validate long range minimum=”18”maximum=”30”/>

<h:input text>

<h: messages for= ”uage” style=”color: red”/>

<h: output text value=”user salary”/>

<h:input text id=”usal” value=”#{ reg bean. usal}”

required=”true” label=”user salary”>

<f:validate Double range minimum=”25000”maximum=”50000”/>

<h:input text>

<h: messages for= ”usal” style=”color: red”/>

<h:command button value=”Registration”action=”#

{reg bean. registration”} ”/>

</h:panel Grid>





<head >


<title>insert title here<1title>




<h1>Registration success</h1>





Package com. soft;

Public class Registration Bean{

Private string uname;

Private string upwd;

Private int uage;

Private floal usal;



Public string registration(){

Return ”success”;





<?xml version=”1.0”encoding=”UTF-8”?>


Xmlns=—-xsd ”version=”1.2”>


<managed-bean-name>reg bean</managed-bean-name >

<managed-bean-class>com. Durga soft. registration B ean</managed-bean- class >

<managed-bean-scope>session</managed-bean- scope > </managed-bean >


<from-view-id>/registration from.jsp</  >


<from-out come>success</  >

<to-view- id>/success.jsp</  >




<message-bundle>abc</   >




Web.xml same as prev .appl.?

Custom validations: In JSF based web application , if all the standard validations are not sufficient to perform data validations, then JSF has given an environment to prepare user defined validations

  • To prepare custom Validator, we have to use the following steps
  1. Prepare user defined Validator class.
  2. Configure custom Validator in faces-config.xml file
  • Use custom valid tore in faces-config.xml component in user forms,

Prepare user defined Validator class. for this we have to take an user defined class & it must implement javax.faces. Validator. Validator interface

  • Validator interface has the following method to provide custom validation logic

Public void validate(faces context fc, UI Component UC Object value)throws Validator Exception;

  • In validate()method we have to provide the following implementation.


  1. a) prepare faces message object to represent a single validation message faces message fm=new faces message();
  2. b) Add validation message to faces message object by using either of the following methods

Public void set detail(string msg)

Public void set summary(string msg)

Msg. set detail(“Invalid mobile number”);

Msg. set summary(“Invalid mobile number”);

  1. c) Rise Validator Exception with the faces message throw new Validator exception(fon)


ii) Configure custom Validator in faces-config.xml files:

To configure custom Validator in faces-config.xml file we have to use the following tags




<Validator-class>custom Validator fully qualified name</




iii) Use custom validator to the required i/p component

to use custom validator with i/p component have to use the following tag

<f:validator validator Id=-validator Id=….”/>

This tag must be used under<h:input text>iag


Custom validator app(1.x custom validator)

Eid=Employee id

ename =Employee name

eaddres =Employee addres

eemail =Employee mail ID

eemobile =Employee mobile


email-validatore=invalid email id

//copy standard Validator msg from messages. Properties file and modify them





<meta http-equiv=”Refresh”content=”2;url=http://local host:100/custom validate app/employee form.dss”>


<h1>application is loading</h1>




Employee form.jsp

%@laglib perfix=”f”url=”http//





<font color=”red”>

<h1> software solutions</h1>

<h2>employee registration form</h2>



<h:panel grid=columns=”3”>

<h:output text value=”#{msg.eid}”/>

<h:input text id=”eid” value=”#{regBean.eid}”required

=”true ”label=”Employee Id”/>

<h:message for=”eid”/>

<h:output text value=”#{msg. ename}”/>

<h:input text id=”ename” value=”#{regBean. ename}”

Required=”true”label=”employee name”/>

<h:message for=”ename”/>

<h:output text value=”#{msg. eaddr}”/>

<h:input text id=”eaddre” value=”#{reg Bean. eaddr}”

Required=”true” label=”employee address”/>

<h:message for=”eaddr”/>

<h: output text value=”#{msg. eemail}”/>

<h:input text id=”eemail” value=”#{reg Bean. eemail}”

Required=”true” label=”employee email”/>

<f:validator validator id=”email validator”/>

</h:input text>

<h:message for=”eemail’/>

<h: output text value=”#{msg. emobile}”/>

<h:input text id=”emobile” value=”#{reg Bean. eemail}”

Required=”true” label=”employee mobile”/>

<h:message for=”emobile”/>

<h:command button value=”#{reg button}”action=”#{

Reg bean. registration}”/>

</h:panel Grid></h:form></f:view>




<%@laglib prefir=”f”—–core”%>

<                            “h”                     html”%>




<h1>Registration success</h1>






Package com. soft

Public class Registration Bean{

Private string eid;

Private string ename;

Private string eaddr;

Private string eemail;

Private string emobile;

//provide setter()&getter()getter()

Private string registration(){

Return ”success”;




Package com. Durga soft;

Import java. util. iterator;

Import java. util. Resource bundle;

Import java. faces. application. application;

Import java. Faces. application. faces message;

Import java. faces. content. faces context;

Import java. faces. Validator. validator;

Import java. faces. validator. validator exception;

Public class email validator implements validator{

Public void validator(faces context fe,UI Component we, object value) throws validator exception{

String email.=(string)value;

If(email. contains(“@”)){

Application application=fe.get application();

String prp-file=application. get message bundle();

Resource bundle bundle=Resource bundle. get bundle(prp-file);

String err-msg=bundle.get string(“email-validator”);

Faces message fm=new faces message();

Fm.set detail(err-msg);

Throw new validator Exception (fm);








—— XSD

<Managed. bean>

< Managed. Bean-name>reg bean</ Managed. Bean-name>

< Managed. Bean-class>com. durga soft. Registration bean reg

</    >

Managed. Bean-scope>session </    >

< /Managed .bean>


<from-view-id>/emkployeeform.jsp</    >


<from-outcome>success</    >

<to-view-id>/ success.jsp</    >



<validator-id>email-validator</    >




<base-name>abc</b >


<message-bundle>abc< /message-bundle >



“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at, we will update the article in 24 hours.”

0 Responses on Steps to provide customized validation messages"

    Leave a Message

    Your email address will not be published. Required fields are marked *


    Please leave a message and we'll get back to you soon.
    Three + 6