<p>hi here is my code in struts i want to validate my form fields but it couldn't work can you fix what mistakes i have done</p> reg.jsp <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <html> <body bgcolor="#996600"> <center><h1>Welcome To Delhi Public School</h1></center> <html:errors/><br/> <html:form action="reg1.do"> <pre> Student Name:<html:text property="Sname" size="20"/> Fathers Name:<html:text property="Fname" size="20"/> Occupation:<html:text property="Occupation" size="20"/> Contact no:<html:text property="contactno" size="20"/> Email Id:<html:text property="Email" size="20"/> DOB:<html:text property="dob" size="20"/> Identification Marks:<html:text property="identifymarks" size="20"/> last school:<html:text property="lastschool" size="20"/> last class:<html:text property="lastclass" size="20"/> Marks:<html:text property="marks" size="20"/> Percentage:<html:text property="Percentage" size="20"/> Entrance Test:<html:text property="EntranceTest" size="20"/> percentage in test:<html:text property="percentageInTest" size="20"/> Present class<html:text property="presentclass" size="20"/> <center>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp <html:submit>SUBMIT</html:submit>&nbsp&nbsp <html:submit>CLEAR</html:submit></center> <pre> </html:form> </body> </html> registeAction.java import org.apache.struts.action.*; import javax.servlet.http.*; import java.sql.*; import java.text.SimpleDateFormat; public class RegisterAction extends Action { public RegisterAction() { try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }//try catch(Exception e) { }//catch }//constructor public ActionForward execute(ActionMapping am,ActionForm af,HttpServletRequest req,HttpServletResponse res)throws Exception { Connection con=null; DynaActionForm daf=(DynaActionForm)af; try{ int std_Id=0; //String d=daf.get("dob"); //DateFormat sdf=new SimpleDateFormat("dd-mm-yyyy"); //Date date=(Date)sdf.parse(d); //java.sql.Date sqldate=new java.sql.Date(new java.util.Date(d).getTime()); con=DriverManager.getConnection("jdbc:odbc:ora","system","manager"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select seq.nextval from dual"); while(rs.next()) { std_Id=rs.getInt(1); }//while PreparedStatement ps=con.prepareStatement("insert into std1_info values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); ps.setInt(1,std_Id); ps.setString(2,(String)daf.get("Sname")); ps.setString(3,(String)daf.get("Fname")); ps.setString(4,(String)daf.get("Occupation")); ps.setInt(5,Integer.parseInt(daf.get("contactno").toString())); ps.setString(6,(String)daf.get("Email")); ps.setDate(7,java.sql.Date.valueOf((String)daf.get("dob"))); ps.setString(8,(String)daf.get("identificationmarks")); ps.setString(9,(String)daf.get("lastschool")); ps.setString(10,(String)daf.get("lastclass")); ps.setInt(11,Integer.parseInt(daf.get("marks").toString())); ps.setDouble(12,Double.parseDouble(daf.get("Percentage").toString())); ps.setString(13,(String)daf.get("EntranceTest")); ps.setDouble(14,Double.parseDouble(daf.get("percentageInTest").toString())); ps.setString(15,(String)daf.get("presentclass")); ps.setString(16,"P"); System.out.println(std_Id); int x=ps.executeUpdate(); if(x==1){ System.out.println("uploaded babe"); return am.findForward("test"); } else{ return am.findForward("failure1"); } }//try finally { con.close(); }//catch }//execute }//class struts-config.xml <struts-config> <form-beans> <form-bean name="regform" type="org.apache.struts.validator.DynaValidatorForm"> <form-property name="Sname" type="java.lang.String"/> <form-property name="Fname" type="java.lang.String"/> <form-property name="Occupation" type="java.lang.String"/> <form-property name="contactno" type="java.lang.String"/> <form-property name="Email" type="java.lang.String"/> <form-property name="dob" type="java.lang.String"/> <form-property name="identificationmarks" type="java.lang.String"/> <form-property name="lastschool" type="java.lang.String"/> <form-property name="lastclass" type="java.lang.String"/> <form-property name="marks" type="java.lang.String"/> <form-property name="Percentage" type="java.lang.String"/> <form-property name="EntranceTest" type="java.lang.String"/> <form-property name="percentageInTest" type="java.lang.String"/> <form-property name="presentclass" type="java.lang.String"/> </form-bean> </form-beans> <action-mappings> <action path="/reg" type="RegisterAction" name="regform" validate="true" scope="session"> <forward name="test" path="/success.html" /> </action> </action-mappings> <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/> </plug-in> </struts-config> validator-rules.xml <!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN" "http://jakarta.apache.org/commons/dtds/validator_1_0.dtd"> <!-- This file contains the default Struts Validator pluggable validator definitions. It should be placed somewhere under /WEB-INF and referenced in the struts-config.xml under the plug-in element for the ValidatorPlugIn. <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml"/> </plug-in> These are the default error messages associated with each validator defined in this file. They should be added to your projects ApplicationResources.properties file or you can associate new ones by modifying the pluggable validators msg attributes in this file. # Struts Validator Error Messages errors.required={0} is required. errors.minlength={0} can not be less than {1} characters. errors.maxlength={0} can not be greater than {1} characters. errors.invalid={0} is invalid. errors.byte={0} must be a byte. errors.short={0} must be a short. errors.integer={0} must be an integer. errors.long={0} must be a long. errors.float={0} must be a float. errors.double={0} must be a double. errors.date={0} is not a date. errors.range={0} is not in the range {1} through {2}. errors.creditcard={0} is an invalid credit card number. errors.email={0} is an invalid e-mail address. --> <form-validation> <global> <validator name="required" classname="org.apache.struts.validator.FieldChecks" method="validateRequired" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest" msg="errors.required"> <javascript><![CDATA[ function validateRequired(form) { var bValid = true; var focusField = null; var i = 0; var fields = new Array(); oRequired = new required(); for (x in oRequired) { var field = form[oRequired[x][0]]; if (field.type == 'text' || field.type == 'textarea' || field.type == 'file' || field.type == 'select-one' || field.type == 'radio' || field.type == 'password') { var value = ''; // get field's value if (field.type == "select-one") { var si = field.selectedIndex; if (si >= 0) { value = field.options[si].value; } } else { value = field.value; } if (value == '') { if (i == 0) { focusField = field; } fields[i++] = oRequired[x][1]; bValid = false; } } } if (fields.length > 0) { focusField.focus(); alert(fields.join('\n')); } return bValid; }]]> </javascript> </validator> <validator name="requiredif" classname="org.apache.struts.validator.FieldChecks" method="validateRequiredIf" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, org.apache.commons.validator.Validator, javax.servlet.http.HttpServletRequest" msg="errors.required"> </validator> <validator name="minlength" classname="org.apache.struts.validator.FieldChecks" method="validateMinLength" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest" depends="" msg="errors.minlength"> <javascript><![CDATA[ function validateMinLength(form) { var bValid = true; var focusField = null; var i = 0; var fields = new Array(); oMinLength = new minlength(); for (x in oMinLength) { if (form[oMinLength[x][0]].type == 'text' || form[oMinLength[x][0]].type == 'textarea') { var iMin = parseInt(oMinLength[x][2]("minlength")); if (form[oMinLength[x][0]].value.length < iMin) { if (i == 0) { focusField = form[oMinLength[x][0]]; } fields[i++] = oMinLength[x][1]; bValid = false; } } } if (fields.length > 0) { focusField.focus(); alert(fields.join('\n')); } return bValid; }]]> </javascript> </validator> <validator name="maxlength" classname="org.apache.struts.validator.FieldChecks" method="validateMaxLength" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest" depends="" msg="errors.maxlength"> <javascript><![CDATA[ function validateMaxLength(form) { var bValid = true; var focusField = null; var i = 0; var fields = new Array(); oMaxLength = new maxlength(); for (x in oMaxLength) { if (form[oMaxLength[x][0]].type == 'text' || form[oMaxLength[x][0]].type == 'textarea') { var iMax = parseInt(oMaxLength[x][2]("maxlength")); if (form[oMaxLength[x][0]].value.length > iMax) { if (i == 0) { focusField = form[oMaxLength[x][0]]; } fields[i++] = oMaxLength[x][1]; bValid = false; } } } if (fields.length > 0) { focusField.focus(); alert(fields.join('\n')); } return bValid; }]]> </javascript> </validator> <validator name="mask" classname="org.apache.struts.validator.FieldChecks" method="validateMask" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest" depends="" msg="errors.invalid"> <javascript><![CDATA[ function validateMask(form) { var bValid = true; var focusField = null; var i = 0; var fields = new Array(); oMasked = new mask(); for (x in oMasked) { if ((form[oMasked[x][0]].type == 'text' || form[oMasked[x][0]].type == 'textarea' || form[oMasked[x][0]].type == 'password') && (form[oMasked[x][0]].value.length > 0)) { if (!matchPattern(form[oMasked[x][0]].value, oMasked[x][2]("mask"))) { if (i == 0) { focusField = form[oMasked[x][0]]; } fields[i++] = oMasked[x][1]; bValid = false; } } } if (fields.length > 0) { focusField.focus(); alert(fields.join('\n')); } return bValid; } function matchPattern(value, mask) { var bMatched = mask.exec(value); if (!bMatched) { return false; } return true; }]]> </javascript> </validator> <validator name="byte" classname="org.apache.struts.validator.FieldChecks" method="validateByte" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest" depends="" msg="errors.byte" jsFunctionName="ByteValidations"> <javascript><![CDATA[ function validateByte(form) { var bValid = true; var focusField = null; var i = 0; var fields = new Array(); oByte = new ByteValidations(); for (x in oByte) { var field = form[oByte[x][0]]; if (field.type == 'text' || field.type == 'textarea' || field.type == 'select-one' || field.type == 'radio') { var value = ''; // get field's value if (field.type == "select-one") { var si = field.selectedIndex; if (si >= 0) { value = field.options[si].value; } } else { value = field.value; } if (value.length > 0) { var iValue = parseInt(value); if (isNaN(iValue) || !(iValue >= -128 && iValue <= 127)) { if (i == 0) { focusField = field; } fields[i++] = oByte[x][1]; bValid = false; } } } } if (fields.length > 0) { focusField.focus(); alert(fields.join('\n')); } return bValid; }]]> </javascript> </validator> <validator name="short" classname="org.apache.struts.validator.FieldChecks" method="validateShort" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest" depends="" msg="errors.short" jsFunctionName="ShortValidations"> <javascript><![CDATA[ function validateShort(form) { var bValid = true; var focusField = null; var i = 0; var fields = new Array(); oShort = new ShortValidations(); for (x in oShort) { var field = form[oShort[x][0]]; if (field.type == 'text' || field.type == 'textarea' || field.type == 'select-one' || field.type == 'radio') { var value = ''; // get field's value if (field.type == "select-one") { var si = field.selectedIndex; if (si >= 0) { value = field.options[si].value; } } else { value = field.value; } if (value.length > 0) { var iValue = parseInt(value); if (isNaN(iValue) || !(iValue >= -32768 && iValue <= 32767)) { if (i == 0) { focusField = field; } fields[i++] = oShort[x][1]; bValid = false; } } } } if (fields.length > 0) { focusField.focus(); alert(fields.join('\n')); } return bValid; }]]> </javascript> </validator> <validator name="integer" classname="org.apache.struts.validator.FieldChecks" method="validateInteger" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest" depends="" msg="errors.integer" jsFunctionName="IntegerValidations"> <javascript><![CDATA[ function validateInteger(form) { var bValid = true; var focusField = null; var i = 0; var fields = new Array(); oInteger = new IntegerValidations(); for (x in oInteger) { var field = form[oInteger[x][0]]; if (field.type == 'text' || field.type == 'textarea' || field.type == 'select-one' || field.type == 'radio') { var value = ''; // get field's value if (field.type == "select-one") { var si = field.selectedIndex; if (si >= 0) { value = field.options[si].value; } } else { value = field.value; } if (value.length > 0) { if (!isAllDigits(value)) { bValid = false; } else { var iValue = parseInt(value); if (isNaN(iValue) || !(iValue >= -2147483648 && iValue <= 2147483647)) { if (i == 0) { focusField = field; } fields[i++] = oInteger[x][1]; bValid = false; } } } } } if (fields.length > 0) { focusField.focus(); alert(fields.join('\n')); } return bValid; } function isAllDigits(argvalue) { argvalue = argvalue.toString(); var validChars = "0123456789"; var startFrom = 0; if (argvalue.substring(0, 2) == "0x") { validChars = "0123456789abcdefABCDEF"; startFrom = 2; } else if (argvalue.charAt(0) == "0") { validChars = "01234567"; startFrom = 1; } for (var n = 0; n < argvalue.length; n++) { if (validChars.indexOf(argvalue.substring(n, n+1)) == -1) return false; } return true; }]]> </javascript> </validator> <validator name="long" classname="org.apache.struts.validator.FieldChecks" method="validateLong" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest" depends="" msg="errors.long"/> <validator name="float" classname="org.apache.struts.validator.FieldChecks" method="validateFloat" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest" depends="" msg="errors.float" jsFunctionName="FloatValidations"> <javascript><![CDATA[ function validateFloat(form) { var bValid = true; var focusField = null; var i = 0; var fields = new Array(); oFloat = new FloatValidations(); for (x in oFloat) { var field = form[oFloat[x][0]]; if (field.type == 'text' || field.type == 'textarea' || field.type == 'select-one' || field.type == 'radio') { var value = ''; // get field's value if (field.type == "select-one") { var si = field.selectedIndex; if (si >= 0) { value = field.options[si].value; } } else { value = field.value; } if (value.length > 0) { var iValue = parseFloat(value); if (isNaN(iValue)) { if (i == 0) { focusField = field; } fields[i++] = oFloat[x][1]; bValid = false; } } } } if (fields.length > 0) { focusField.focus(); alert(fields.join('\n')); } return bValid; }]]> </javascript> </validator> <validator name="double" classname="org.apache.struts.validator.FieldChecks" method="validateDouble" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest" depends="" msg="errors.double"/> <validator name="date" classname="org.apache.struts.validator.FieldChecks" method="validateDate" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest" depends="" msg="errors.date" jsFunctionName="DateValidations"> <javascript><![CDATA[ function validateDate(form) { var bValid = true; var focusField = null; var i = 0; var fields = new Array(); oDate = new DateValidations(); for (x in oDate) { var value = form[oDate[x][0]].value; var datePattern = oDate[x][2]("datePatternStrict"); if ((form[oDate[x][0]].type == 'text' || form[oDate[x][0]].type == 'textarea') && (value.length > 0) && (datePattern.length > 0)) { var MONTH = "MM"; var DAY = "dd"; var YEAR = "yyyy"; var orderMonth = datePattern.indexOf(MONTH); var orderDay = datePattern.indexOf(DAY); var orderYear = datePattern.indexOf(YEAR); if ((orderDay < orderYear && orderDay > orderMonth)) { var iDelim1 = orderMonth + MONTH.length; var iDelim2 = orderDay + DAY.length; var delim1 = datePattern.substring(iDelim1, iDelim1 + 1); var delim2 = datePattern.substring(iDelim2, iDelim2 + 1); if (iDelim1 == orderDay && iDelim2 == orderYear) { dateRegexp = new RegExp("^(\\d{2})(\\d{2})(\\d{4})$"); } else if (iDelim1 == orderDay) { dateRegexp = new RegExp("^(\\d{2})(\\d{2})[" + delim2 + "](\\d{4})$"); } else if (iDelim2 == orderYear) { dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})(\\d{4})$"); } else { dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})$"); } var matched = dateRegexp.exec(value); if(matched != null) { if (!isValidDate(matched[2], matched[1], matched[3])) { if (i == 0) { focusField = form[oDate[x][0]]; } fields[i++] = oDate[x][1]; bValid = false; } } else { if (i == 0) { focusField = form[oDate[x][0]]; } fields[i++] = oDate[x][1]; bValid = false; } } else if ((orderMonth < orderYear && orderMonth > orderDay)) { var iDelim1 = orderDay + DAY.length; var iDelim2 = orderMonth + MONTH.length; var delim1 = datePattern.substring(iDelim1, iDelim1 + 1); var delim2 = datePattern.substring(iDelim2, iDelim2 + 1); if (iDelim1 == orderMonth && iDelim2 == orderYear) { dateRegexp = new RegExp("^(\\d{2})(\\d{2})(\\d{4})$"); } else if (iDelim1 == orderMonth) { dateRegexp = new RegExp("^(\\d{2})(\\d{2})[" + delim2 + "](\\d{4})$"); } else if (iDelim2 == orderYear) { dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})(\\d{4})$"); } else { dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})$"); } var matched = dateRegexp.exec(value); if(matched != null) { if (!isValidDate(matched[1], matched[2], matched[3])) { if (i == 0) { focusField = form[oDate[x][0]]; } fields[i++] = oDate[x][1]; bValid = false; } } else { if (i == 0) { focusField = form[oDate[x][0]]; } fields[i++] = oDate[x][1]; bValid = false; } } else if ((orderMonth > orderYear && orderMonth < orderDay)) { var iDelim1 = orderYear + YEAR.length; var iDelim2 = orderMonth + MONTH.length; var delim1 = datePattern.substring(iDelim1, iDelim1 + 1); var delim2 = datePattern.substring(iDelim2, iDelim2 + 1); if (iDelim1 == orderMonth && iDelim2 == orderDay) { dateRegexp = new RegExp("^(\\d{4})(\\d{2})(\\d{2})$"); } else if (iDelim1 == orderMonth) { dateRegexp = new RegExp("^(\\d{4})(\\d{2})[" + delim2 + "](\\d{2})$"); } else if (iDelim2 == orderDay) { dateRegexp = new RegExp("^(\\d{4})[" + delim1 + "](\\d{2})(\\d{2})$"); } else { dateRegexp = new RegExp("^(\\d{4})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{2})$"); } var matched = dateRegexp.exec(value); if(matched != null) { if (!isValidDate(matched[3], matched[2], matched[1])) { if (i == 0) { focusField = form[oDate[x][0]]; } fields[i++] = oDate[x][1]; bValid = false; } } else { if (i == 0) { focusField = form[oDate[x][0]]; } fields[i++] = oDate[x][1]; bValid = false; } } else { if (i == 0) { focusField = form[oDate[x][0]]; } fields[i++] = oDate[x][1]; bValid = false; } } } if (fields.length > 0) { focusField.focus(); alert(fields.join('\n')); } return bValid; } function isValidDate(day, month, year) { if (month < 1 || month > 12) { return false; } if (day < 1 || day > 31) { return false; } if ((month == 4 || month == 6 || month == 9 || month == 11) && (day == 31)) { return false; } if (month == 2) { var leap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); if (day>29 || (day == 29 && !leap)) { return false; } } return true; }]]> </javascript> </validator> <!-- range is deprecated use intRange instead --> <validator name="range" classname="org.apache.struts.validator.FieldChecks" method="validateIntRange" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest" depends="integer" msg="errors.range"> <javascript><![CDATA[ function validateRange(form) { return validateIntRange(form); }]]> </javascript> </validator> <validator name="intRange" classname="org.apache.struts.validator.FieldChecks" method="validateIntRange" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest" depends="integer" msg="errors.range"> <javascript><![CDATA[ function validateIntRange(form) { var bValid = true; var focusField = null; var i = 0; var fields = new Array(); oRange = new intRange(); for (x in oRange) { if ((form[oRange[x][0]].type == 'text' || form[oRange[x][0]].type == 'textarea') && (form[oRange[x][0]].value.length > 0)) { var iMin = parseInt(oRange[x][2]("min")); var iMax = parseInt(oRange[x][2]("max")); var iValue = parseInt(form[oRange[x][0]].value); if (!(iValue >= iMin && iValue <= iMax)) { if (i == 0) { focusField = form[oRange[x][0]]; } fields[i++] = oRange[x][1]; bValid = false; } } } if (fields.length > 0) { focusField.focus(); alert(fields.join('\n')); } return bValid; }]]> </javascript> </validator> <validator name="floatRange" classname="org.apache.struts.validator.FieldChecks" method="validateFloatRange" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest" depends="float" msg="errors.range"> <javascript><![CDATA[ function validateFloatRange(form) { var bValid = true; var focusField = null; var i = 0; var fields = new Array(); oRange = new floatRange(); for (x in oRange) { if ((form[oRange[x][0]].type == 'text' || form[oRange[x][0]].type == 'textarea') && (form[oRange[x][0]].value.length > 0)) { var fMin = parseFloat(oRange[x][2]("min")); var fMax = parseFloat(oRange[x][2]("max")); var fValue = parseFloat(form[oRange[x][0]].value); if (!(fValue >= fMin && fValue <= fMax)) { if (i == 0) { focusField = form[oRange[x][0]]; } fields[i++] = oRange[x][1]; bValid = false; } } } if (fields.length > 0) { focusField.focus(); alert(fields.join('\n')); } return bValid; }]]> </javascript> </validator> <validator name="creditCard" classname="org.apache.struts.validator.FieldChecks" method="validateCreditCard" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest" depends="" msg="errors.creditcard"> <javascript><![CDATA[ function validateCreditCard(form) { var bValid = true; var focusField = null; var i = 0; var fields = new Array(); oCreditCard = new creditCard(); for (x in oCreditCard) { if ((form[oCreditCard[x][0]].type == 'text' || form[oCreditCard[x][0]].type == 'textarea') && (form[oCreditCard[x][0]].value.length > 0)) { if (!luhnCheck(form[oCreditCard[x][0]].value)) { if (i == 0) { focusField = form[oCreditCard[x][0]]; } fields[i++] = oCreditCard[x][1]; bValid = false; } } } if (fields.length > 0) { focusField.focus(); alert(fields.join('\n')); } return bValid; } /** * Reference: http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl */ function luhnCheck(cardNumber) { if (isLuhnNum(cardNumber)) { var no_digit = cardNumber.length; var oddoeven = no_digit & 1; var sum = 0; for (var count = 0; count < no_digit; count++) { var digit = parseInt(cardNumber.charAt(count)); if (!((count & 1) ^ oddoeven)) { digit *= 2; if (digit > 9) digit -= 9; }; sum += digit; }; if (sum == 0) return false; if (sum % 10 == 0) return true; }; return false; } function isLuhnNum(argvalue) { argvalue = argvalue.toString(); if (argvalue.length == 0) { return false; } for (var n = 0; n < argvalue.length; n++) { if ((argvalue.substring(n, n+1) < "0") || (argvalue.substring(n,n+1) > "9")) { return false; } } return true; }]]> </javascript> </validator> <validator name="email" classname="org.apache.struts.validator.FieldChecks" method="validateEmail" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest" depends="" msg="errors.email"> <javascript><![CDATA[ function validateEmail(form) { var bValid = true; var focusField = null; var i = 0; var fields = new Array(); oEmail = new email(); for (x in oEmail) { if ((form[oEmail[x][0]].type == 'text' || form[oEmail[x][0]].type == 'textarea') && (form[oEmail[x][0]].value.length > 0)) { if (!checkEmail(form[oEmail[x][0]].value)) { if (i == 0) { focusField = form[oEmail[x][0]]; } fields[i++] = oEmail[x][1]; bValid = false; } } } if (fields.length > 0) { focusField.focus(); alert(fields.join('\n')); } return bValid; } /** * Reference: Sandeep V. Tamhankar ([email protected]), * http://javascript.internet.com */ function checkEmail(emailStr) { if (emailStr.length == 0) { return true; } var emailPat=/^(.+)@(.+)$/; var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"; var validChars="\[^\\s" + specialChars + "\]"; var quotedUser="(\"[^\"]*\")"; var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/; var atom=validChars + '+'; var word="(" + atom + "|" + quotedUser + ")"; var userPat=new RegExp("^" + word + "(\\." + word + ")*$"); var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$"); var matchArray=emailStr.match(emailPat); if (matchArray == null) { return false; } var user=matchArray[1]; var domain=matchArray[2]; if (user.match(userPat) == null) { return false; } var IPArray = domain.match(ipDomainPat); if (IPArray != null) { for (var i = 1; i <= 4; i++) { if (IPArray[i] > 255) { return false; } } return true; } var domainArray=domain.match(domainPat); if (domainArray == null) { return false; } var atomPat=new RegExp(atom,"g"); var domArr=domain.match(atomPat); var len=domArr.length; if ((domArr[domArr.length-1].length < 2) || (domArr[domArr.length-1].length > 3)) { return false; } if (len < 2) { return false; } return true; }]]> </javascript> </validator> </global> </form-validation> web.xml <web-app> <servlet> <servlet-name>AS</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AS</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>welcome.html</welcome-file> </welcome-file-list> <taglib> <taglib-uri>/tags/struts-html</taglib-uri> <taglib-location>/WEB-INF/struts-html.tld</taglib-location> </taglib> <taglib> <taglib-uri>/tags/struts-bean</taglib-uri> <taglib-location>/WEB-INF/struts-bean.tld</taglib-location> </taglib> </web-app> success.html you have successfully regesterd failure.html try again Applicationresources.Properties regform.Sname=<b> Student Name</b> regform.Fname=<b> Father Name</b> regform.Occupation=<b>Occupation</b> regform.contactno=<b>Contact Number</b> regform.Email=<b>Email</b> regform.dob=<b>Date Of Birth</b> regform.identificationmarks=<b>Identification Marks</b> regform.lastschool=<b>Last School Studied</b> regform.lastclass=<b>Last Class</b> regform.marks=<b>Marks</b> regform.Percentage=<b>percentage</b> regform.EntranceTest=<b>Entrence Test</b> regform.percentageInTest=<b>Percentage In Test</b> regform.presentclass=<b>Present Class</b> errors.required=<i><li>{0} field can no be empty</li></i><br/> errors.integer=<i><li>{0} must be an Integer</li></i><br/> errors.email=<i><li>{0} is an invalid email address</li></i><br/> errors.date=<i><li>{0} field can no be empty</li></i><br/> errors.integer=<i><li>{0} must be a number</li></i><br/> errors.double=<i><li>{0} must be a number</li></i><br/>
Ads