Using Client Side Java Script Validation

If we want the validators to act at client side using that is to perform validations using JavaScript then include the <html:javascript>element in the jsp document. The <html:javascript> elements formName attribute takes the form bean name that has to be validate. The example of <htmlLjavascript>element is shown in following

<html:javascript formName=”registerform”/>

Apart from adding the <html:javascript> element we need to add the following onsubmit attribute in <html:form>tag,which takes return validate <form bean name> (this);. The following snippet shows the sample of <html:form>

<html:form action=”register” onsubmit=”return validateRegisterfrom(this);”>

Now, modifying the Registration page of the preceding example to use client side validations, the modified Registration.jsp is shown in  here

Registration.jsp
<html>
<head>
<html:form action=”register” onsubmit=”return validateREgisterform(this);”>
<table>
<tr>
<td>UserName</td>
<td><html:text property=”uname” size=”25”/></td>
</tr>
<tr>
<td>Password</td>
<td><html:password property=”pass” size=”25”/></td>
</tr>
<tr>
<td>Email</td>
<td><html:text property=”email” size=”25”/></td>
</tr>
<tr>
<td>Address</td>
<td><html:text property=”address” size=”25”/></td>
</tr>
<tr>
<td>Mobile</td>
<td><html:text property=”mobile’ size=”25”/></td>
</tr>
<tr>
<td> colspan=2><html:submit>Register</html:submit></td>
</tr>
</table>
</html:form>
</body></html>

The output of the preceding example when using the Registration.jsp given bellow

<li><i><b>User Name</b>field cannot be empty</i></li><br/>
<li><i><b>Password</b>field cannot be empty</i></li><br/>
<li><i><b>Email Id</b>field cannot be empty</i></li><br/>
<li><i><b>Address</b>field cannot be empty</i></li><br/>

Difference between ValidatorForm & ValidatorActionForm

The only difference between the ValidatorForm and ValidatorActionForm is that the ValidatorFrom locates the validation rules based on the form bean name, where as validatorActionForm locates the validation rules based on the requested action path.

The ValidatorActionForm is useful when we associate a single form bean to multiple actions and if each of these actions requires different validations. In this case, while defining the validation rules we need to define the validation.xml file’s<form> element name attribute with the action path instead of form bean name.

The difference between DynaValidatorFrom and DynaValidatorActionForm is also same as described above.


Post a Comment

Anonymous said... June 6, 2013 at 1:16 AM

where r u sir.......this is ramu

javapracticalinfo said... June 26, 2013 at 4:19 AM

i am in hyderabad. This is my contact no :98660307742

 
Top