validation rules that can be used in the web applications. The client-side validation in Struts is well known. Some of the available features: required requiredif validwhen minlength maxlength mask byte short integer long float Double creditCard email url byteLocale shortLocal e integerLoc ale longLocale floatLocale doubleLoc ale date intRange longRange floatRange doubleRan ge
Edit validator-rules.xml and add the required code for the new rule.
Creating a new Validator matchname is the new validator created Error message issued in the browser <javascript><![CDATA[ function validateName(form) { var isValid = true; var focusField = null; var i = 0; var fields = new Array(); var omatchName= eval('new ' + jcv_retrieveFormName(form) + '_matchname() '); for (var x in omatchName) { if (!jcv_verifyArrayElement(x, omatchName[x])) { continue; } var field = form[omatchName[x][0]];
if (!jcv_isFieldPresent(field)) { fields[i++] = omatchName[x][1]; isValid=false; } else if (field.value != "administrator") { fields[i++]=omatchName[x][1]; isValid=false; } } if (fields.length > 0) { jcv_handleErrors(fields, focusField); } return isValid; } ]]> </javascript> </validator> Test the value entered in the browser Edit validation.xml and add: <!-- Name form Validation--> <form-validation> <formset> <form name="AdminForm"> <field property="name" depends="matchname"> <arg0 key="AddressForm.name"/> </field> </form> </formset> </form-validation> Edit struts-configuration.xml and add: form-bean name="AdminForm" type="test.AdminForm"/> <action path="/AdminFormValidation" type="test.AdminForm" name="AdminForm" scope="request" validate="true" input="admin.jsp"> <forward name="success" path="success.jsp"/> </action>
<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> Create the Java Class for the AdminForm Create the success.jsp Create the AdminAction.java