DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. JavaScript
  4. Struts Client-Side JavaScript Validation Tutorial

Struts Client-Side JavaScript Validation Tutorial

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 15, 12 · Tutorial
Like (0)
Save
Tweet
Share
75.61K Views

Join the DZone community and get the full member experience.

Join For Free

Struts Validator Framework provides an easy-to-use mechanism for performing client-side validation. It's very useful to validate some fields on the client-side before sending the data to the server for processing. By this way we can ensure that the data send to the server is valid. Performing validations on the client-side save the user a round trip time to the server.

For each validation routine defined in the validation-rules.xml file Struts provides an optional JavaScript code that can run on the client-side to perform the same validation that takes place on the server side.

Let's take a login application for our example. Our LoginForm extends DynaValidatorForm.

<form-bean name="LoginForm" type="org.apache.struts.validator.DynaValidatorForm">
    <form-property name="userName" type="java.lang.String" />
    <form-property name="password" type="java.lang.String" />
</form-bean>

The following validations are defined in the validation.xml file.

<form name="LoginForm">
    <field property="userName" depends="required">
    	<arg key="LoginForm.userName"/>
    </field>
    <field property="password" depends="required,minlength">
        <arg0 key="LoginForm.password"/>
        <arg1 key="${var:minlength}" name="minlength" resource="false"/>
        <var>
            <var-name>minlength</var-name>
            <var-value>6</var-value>
        </var>
    </field>
</form>

To enable client-side validation you have to place the Struts HTML Tag Library's javascript tag in each jsp page for which you need to preform client-side validation.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JS Validation</title>
</head>
<body>
    <html:form action="/Login" onsubmit="validateLoginForm(this);">>
        <html:javascript formName="LoginForm" />
        User Name : <html:text name="LoginForm" property="userName" /> <br>
        Password  : <html:password name="LoginForm" property="password" /> <br>
        <html:submit value="Login" />
    </html:form>
</body>
</html>

The formName property of the javascript tag hold the name of the form specified in the validation.xml file.

Next step is to specify the onsubmit attribute of the HTML Tag Library's form tag. The onsubmit attribute is used to specify the javascript code that should be executed when the form is submitted.

The name of the validate method generated by the javascipt tag is formed by concatenating "validate" with the name of the form specified in the javascript. For example, our form name is "LoginForm" so the generated method name will be "validateLoginForm". If the form name is "loginForm", the generated method name will be "validateLoginForm"

Run the application. The login.jsp page will be displayed. Click the login button without entering the User Name and the Passowrd. The following error messages will be displayed to the user. By using the client-side javascript validation we can display the error messages on the alert box, instead of displaying it on the web page.

Enter only the user name and click the Login button. The following error message will be displayed.

Enter a User Name and a Password less than six characters. The following error message will be displayed.

Enter the User Name as "eswar" and a Password greater than six characters. The following success page will be displayed.

You can download the source code of the Struts Javascript validation example by clicking on the Download link below.

Source: Download

Source + Lib: Download

JavaScript

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Beyond Coding: The 5 Must-Have Skills to Have If You Want to Become a Senior Programmer
  • Use AWS Controllers for Kubernetes To Deploy a Serverless Data Processing Solution With SQS, Lambda, and DynamoDB
  • 7 Ways for Better Collaboration Among Your Testers and Developers
  • Integrating AWS Secrets Manager With Spring Boot

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: