To validates the user
input on the browser,validator Framework emits
the JavaScript code. Follow these steps to accomplish this:
Enabling the
Validator plug-in: This makes
Validator available to the system itself.
Create Message
Resources To display error
message to the user.
Developing the
Validation rulesIn validation.xml,we have to define the
validation rules for the address form. Struts Validator Framework
uses this rule for generating JavaScript
for validation.
Applying the
rules: For generation of
JavaScript,we are required to add the
appropriate tag to JSP.
Build and test:
Before the testing,we are required to build the application
once the above steps are done .
Enabling the Validator plug- in
Enabling the Validator plug- in is used to enable validator plug-in open the file struts-config.xml and make sure that following line is present in the file.
To generate the validation error,messagesMessage resources are used by Validator Framework .
We need to define the message for name,Address and E-mail address in our application.
Add following lines:
AddressForm.name=Name
AddressForm.address=Address
AddressForm.emailAddress=E-mail address
into the Struts\strutstutorial\web\WEB-INF\MessageResources.properties file.
Developing Validation rules
We are adding only one validation that fields on the form should not be blank in this application.
Add the following code in validation.xml to validate the form.
The above example
defines validation for the form fields
name, address and emailAddress.
To check that the fields are
not left blank,the attribute depends="required"
is used to instructs Validator Framework to generate
the JavaScript .JavaScript shows error message,if the fields are left blank
.The message are taken from the key
defined in <arg key=".."/> tag in the error message.We can take the value from message resources (Struts\strutstutorial\web\WEB-INF\MessageResources.properties).
Applying Validation rules to JSP
To test the application just create AddressJavascriptValidation.jsp file .
The following file called AddressJavascriptValidation.jsp is as follows:
<div align="left">
<p>
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.
</p>
<!-- Begin Validator Javascript Function--> <html:javascript formName="AddressForm"/>
<!-- End of Validator Javascript Function-->
</html:form>
</body>
</html:html>
To plug-in
Validator JavaScript,the code <html:javascript
formName="AddressForm"/> is used .
In struts-config.xml,create the following entry
for the mapping the
/AddressJavascriptValidation url for
handling the form submission through
AddressJavascriptValidation.jsp.
To call the form,add the following line in index.jsp.
<li>
<html:link page="/pages/AddressJavascriptValidation.jsp">Client
Side Validation for Address Form</html:link>
<br>
The Address Form that validates the data on the
client side using Stuts Validator generated
JavaScript.
</li>
Building Example and Testing
Go to Struts\strutstutorial directory and type ant on the command prompt to build and deploy application.
This will use to deploy the application.Then open the browser and navigate to AddressJavascriptValidation.jsp page.
Your browser should shows the following output.
Browser shows the error message,if the fields are left blank and Save button is clicked,
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords:
clientside validation in struts,regular expression validation.regular expressions validation,xml validation,struts framework,struts validator,javascript validation,struts examples,struts example,struts xml,struts jakarta