Procedure to perform client side programmatic form validation with respect to StrutsDemoApp1-pv application:-
Step1:- write java script code as shown below in the form page of Struts application (register.jsp)
In register.jsp:-
- <%@
- <%@
- <%@
- <html>
- <head>
- <script language = “Javascript”>
- Function myvalidate (frm)
- {
- //read form data
- Var unval = frm.username.value;
- Var pwdval = frm.password.value;
- //write programmatic client-side form validation logic
- If (unval ==””)
- {
- Alert (“username is required”);
- Frm.username.focus ();
- Return false:
- }
- Else {
- Var fchar = unval.charAt (0);
- If (! is NaN (fchar))
- {
- Alert (“username must begin with alphabet”);
- Frm.username.focus ();
- Frm.username.value=””;
- Return false;
- }
- }
- If (pwdval == “”)
- {
- Alert (“password is required”);
- Frm.password.focus ();
- Return false;
- }
- Else {
- Var fchar = pwdval.charAt (0);
- If (! is NaN (fchar))
- {
- Alert (“password must begin with alphabet”);
- Frm.password.focus ();
- Frm.password.value=””;
- Return false;
- }
- }
- Return true;
- } //my validate (-,-)
- </script>
- </head>
< html:from action = “register” method = “get” onsubmit = “return myvalidate (this)”>
Username: < html: text property= “username”/> < html: errors property= “unerr”/><br>
Password: < html: password property= “password”/> < html: errors property= “pwderr”/><br>
<html: submit value= “checkDetails”/>
</html>
Result is : <logic: not Empty name = “msg”>
<bean: wrote name = “msg”>
</logic: not Empty>
Step2: – Develop the remaining resources
- Validation plugin supplies sct of readily available form validation logic as form validation rules having both java codes, java script code to perform client side & server side form validation in declaration mode.
- A plug-in is a patch s/w\s/w application that can enhance the functionality of existing s/w or s/w applications.
- In Struts environment validation plug-in, tiles plug-in’s are supplied to enhance the functionalities of Struts applications.
- In Struts environment every plug-in contains one plug-in class name implementing org.apache.Struts.action.plug-in interface.
- When validation plug-in is used for client side form validations the relevant javascript code will be activated when validate plug-in is used for server side form validation the relevant java code will be activated.
The Plug-in class name of validation plug-in is:–
Org.apache.Struts.validator.validatorplug-in
The Plug-in class name of tiles plug-in is:–
Org.apache.Struts.tiles.Tilesplug-in
- To use any plug-in in our Struts application its plug-in class must be configured in Struts configuration file as shown below.
In s-c.xml:-
<plug-in classname = “org.apache.Struts.validator.validatorplug-in”>
<set-property property = “pathnames” value = “/WEB-INF/ validator-rules.xml, /WEB-INF/ validation.xml”/>
- Declarative form validation means the programmer uses already available form validation logic / rules of validator plug-in to validate the form data.
- Validator plug-in supplied validator rules are required maxlength, minlingth, date, email, credit card and etc. since validator rules are predefined while working with validator plug-in.
- All validator rules related details configuration are there in validator rules.xml file. This file can be collected from Struts-home \ lib \ Struts-core-1.3.8.jar file.