Share on Google+Share on Google+

Client Side Address Validation in Struts

In this lesson we will create JSP page for entering the address and use the functionality provided by Validator Framework to validate the user data on the browser.

Client Side Address Validation in Struts


In this lesson we will create JSP page for entering the address and use the functionality provided by Validator Framework to validate the user data on the browser. Validator Framework emits the JavaScript code which validates the user input on the browser. To accomplish this we have to follow the following steps:

  1. Enabling the Validator plug-in: This makes the Validator available to the system.
  2. Create Message Resources for the displaying the error message to the user.
  3. Developing the Validation rules We have to define the validation rules in the validation.xml for the address form. Struts Validator Framework uses this rule for generating the JavaScript for validation.
  4. Applying the rules: We are required to add the appropriate tag to the JSP for generation of JavaScript.
  5. Build and test: We are required to build the application once the above steps are done before testing. 

Enabling the Validator plug- in
To enable the validator plug-in open the file struts-config.xml and make sure that following line is present in the file.

<!--  Validator plugin -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">

Creating Message Resources
Message resources are used by the Validator Framework to generate the validation error messages. In our application we need to define the messages for name, Address and E-mail address. Open the Struts\strutstutorial\web\WEB-INF\ file and add the following lines:
AddressForm.emailAddress=E-mail address

Developing Validation rules
In this application we are adding only one validation that the fields on the form should not be blank.  Add the following code in the validation.xml. 

<!-- Address form Validation-->
<form name="AddressForm">
  <field  property="name"
   <arg key=""/>
  <field  property="address"
   <arg key="AddressForm.address"/>
   <field  property="emailAddress"
  <arg key="AddressForm.emailAddress"/>

The above definition defines the validation for the form fields name, address and emailAddress. The attribute depends="required" instructs the Validator Framework to generate the JavaScript that checks that the fields are not left blank. If the fields are left blank then JavaScript shows the error message. In the error message the message are taken from the key defined in the <arg key=".."/> tag. The value of key is taken from the message resources (Struts\strutstutorial\web\WEB-INF\

Applying Validation rules to JSP
Now create the AddressJavascriptValidation.jsp file to test the application. The code for AddressJavascriptValidation.jsp is as follows:

<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>

<html:html locale="true">
  <title><bean:message key="welcome.title"/></title>
<body bgcolor="white">
<html:form action="/AddressJavascriptValidation" method="post" onsubmit="return validateAddressForm(this);">

<div align="left">
This application shows the use of Struts Validator.<br>
The following form contains fields that are processed by Struts Validator.<br> 
Fill in the form and see how JavaScript generated by Validator Framework validates the form.


<td align="center" colspan="2">
<font size="4"><b>Please Enter the Following Details</b></font>
<td align="right">
<td align="left">
<html:text property="name" size="30" maxlength="30"/>
<td align="right">
<td align="left">
<html:text property="address" size="30" maxlength="30"/>

<td align="right">
<b>E-mail address</b>
<td align="left">
<html:text property="emailAddress" size="30" maxlength="30"/>

<td align="right">
<td align="left">

<!-- Begin Validator Javascript Function-->
<html:javascript formName="AddressForm"/>
<!-- End of Validator Javascript Function-->


The code <html:javascript formName="AddressForm"/> is used to plug-in the Validator JavaScript.

Create the following entry in the struts-config.xml for the mapping the /AddressJavascriptValidation url for handling the form submission through AddressJavascriptValidation.jsp.

   <forward name="success" path="/pages/success.jsp"/>

Add the following line in the index.jsp to call the form.

<html:link page="/pages/AddressJavascriptValidation.jsp">Client Side Validation for Address Form</html:link>
The Address Form that validates the data on the client side using Stuts Validator generated JavaScript.

Building Example and Testing

To build and deploy the application go to Struts\strutstutorial directory and type ant on the command prompt. This will deploy the application. Open the browser and navigate to the AddressJavascriptValidation.jsp page. Your browser should show the following out put.

If the fields are left blank and Save button is clicked, browser shows the error message.

In this lesson you learned how to use Struts Validator Framework to validate the form on client browser.


Posted on: February 12, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Client Side Address Validation in Struts   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
April 15, 2011
mask validation

How to do mask validation for first 4 characters alphabets and remaining are to be numbers??
January 16, 2012

Keep these articles coimng as they've opened many new doors for me.
April 17, 2013

i am getting at error org.apache.jasper.JasperException: An exception occurred processing JSP page /pages/AddressJavascriptValidation.jsp at line 10 7: <html:base/> 8: </head> 9: <body bgcolor="white"> 10: <html:form action="/AddressJavascriptValidation" method="post" onsubmit="return validateAddressForm(this);"> 11: 12: <div align="left"> 13: <p> Also the contents of is not given Hope to get a solution soon