Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


pathan jilani
struts
0 Answer(s)      3 years and 4 months ago
Posted in : Struts


<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

&lt;%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%&gt;
&lt;html&gt;

&lt;body bgcolor="#996600"&gt;
&lt;center&gt;&lt;h1&gt;Welcome To Delhi Public School&lt;/h1&gt;&lt;/center&gt;

&lt;html:errors/&gt;&lt;br/&gt;

&lt;html:form action="reg1.do"&gt;
&lt;pre&gt;
 Student Name:&lt;html:text property="Sname" size="20"/&gt;
Fathers Name:&lt;html:text property="Fname" size="20"/&gt;
Occupation:&lt;html:text property="Occupation" size="20"/&gt;
Contact no:&lt;html:text property="contactno" size="20"/&gt;
Email Id:&lt;html:text property="Email" size="20"/&gt;
DOB:&lt;html:text property="dob" size="20"/&gt;
Identification Marks:&lt;html:text property="identifymarks" size="20"/&gt;
last school:&lt;html:text property="lastschool" size="20"/&gt;
last class:&lt;html:text property="lastclass" size="20"/&gt;
Marks:&lt;html:text property="marks" size="20"/&gt;
Percentage:&lt;html:text property="Percentage" size="20"/&gt;
Entrance Test:&lt;html:text property="EntranceTest" size="20"/&gt;
percentage in test:&lt;html:text property="percentageInTest" size="20"/&gt;
Present class&lt;html:text property="presentclass" size="20"/&gt;
&lt;center&gt;&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp
&lt;html:submit&gt;SUBMIT&lt;/html:submit&gt;&amp;nbsp&amp;nbsp
&lt;html:submit&gt;CLEAR&lt;/html:submit&gt;&lt;/center&gt;
&lt;pre&gt;
&lt;/html:form&gt;
&lt;/body&gt;
&lt;/html&gt;

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

&lt;struts-config&gt;
&lt;form-beans&gt;


&lt;form-bean name="regform" type="org.apache.struts.validator.DynaValidatorForm"&gt;
&lt;form-property name="Sname" type="java.lang.String"/&gt;
&lt;form-property name="Fname" type="java.lang.String"/&gt;
&lt;form-property name="Occupation" type="java.lang.String"/&gt;
&lt;form-property name="contactno" type="java.lang.String"/&gt;
&lt;form-property name="Email" type="java.lang.String"/&gt;
&lt;form-property name="dob" type="java.lang.String"/&gt;
&lt;form-property name="identificationmarks" type="java.lang.String"/&gt;
&lt;form-property name="lastschool" type="java.lang.String"/&gt;
&lt;form-property name="lastclass" type="java.lang.String"/&gt;
&lt;form-property name="marks" type="java.lang.String"/&gt;
&lt;form-property name="Percentage" type="java.lang.String"/&gt;
&lt;form-property name="EntranceTest" type="java.lang.String"/&gt;
&lt;form-property name="percentageInTest" type="java.lang.String"/&gt;
&lt;form-property name="presentclass" type="java.lang.String"/&gt;
&lt;/form-bean&gt;


&lt;/form-beans&gt;
&lt;action-mappings&gt;
&lt;action path="/reg" type="RegisterAction" name="regform" validate="true" scope="session"&gt;
&lt;forward name="test" path="/success.html" /&gt;
&lt;/action&gt;
&lt;/action-mappings&gt;
&lt;plug-in  className="org.apache.struts.validator.ValidatorPlugIn"&gt;
&lt;set-property property="pathnames" 
              value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/&gt;
&lt;/plug-in&gt;


 </struts-config>

validator-rules.xml

&lt;!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"&gt;
&lt;!--

   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.

      &lt;plug-in className="org.apache.struts.validator.ValidatorPlugIn"&gt;
        &lt;set-property property="pathnames" value="/WEB-INF/validator-rules.xml,
                                                  /WEB-INF/validation.xml"/&gt;
      &lt;/plug-in&gt;

   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.

--&gt;

&lt;form-validation&gt;

   &lt;global&gt;

      &lt;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"&gt;

         &lt;javascript&gt;&lt;![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 &gt;= 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 &gt; 0) {
                   focusField.focus();
                   alert(fields.join('\n'));
                }
                return bValid;
            }]]&gt;
         &lt;/javascript&gt;

      &lt;/validator&gt;

      &lt;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"&gt;
      &lt;/validator&gt;

      &lt;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"&gt;

         &lt;javascript&gt;&lt;![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 &lt; iMin) {
                            if (i == 0) {
                                focusField = form[oMinLength[x][0]];
                            }
                            fields[i++] = oMinLength[x][1];
                            bValid = false;
                        }
                    }
                }
                if (fields.length &gt; 0) {
                   focusField.focus();
                   alert(fields.join('\n'));
                }
                return bValid;
            }]]&gt;
         &lt;/javascript&gt;

      &lt;/validator&gt;


      &lt;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"&gt;

         &lt;javascript&gt;&lt;![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 &gt; iMax) {
                            if (i == 0) {
                                focusField = form[oMaxLength[x][0]];
                            }
                            fields[i++] = oMaxLength[x][1];
                            bValid = false;
                        }
                    }
                }
                if (fields.length &gt; 0) {
                   focusField.focus();
                   alert(fields.join('\n'));
                }
                return bValid;
            }]]&gt;
         &lt;/javascript&gt;

      &lt;/validator&gt;


      &lt;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"&gt;

         &lt;javascript&gt;&lt;![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') &amp;&amp;
                        (form[oMasked[x][0]].value.length &gt; 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 &gt; 0) {
                   focusField.focus();
                   alert(fields.join('\n'));
                }
                return bValid;
            }

            function matchPattern(value, mask) {
               var bMatched = mask.exec(value);
               if (!bMatched) {
                   return false;
               }
               return true;
            }]]&gt;
         &lt;/javascript&gt;

      &lt;/validator&gt;


      &lt;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"&gt;

         &lt;javascript&gt;&lt;![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 &gt;= 0) {
                                value = field.options[si].value;
                            }
                        } else {
                            value = field.value;
                        }

                        if (value.length &gt; 0) {

                            var iValue = parseInt(value);
                            if (isNaN(iValue) || !(iValue &gt;= -128 &amp;&amp; iValue &lt;= 127)) {
                                if (i == 0) {
                                    focusField = field;
                                }
                                fields[i++] = oByte[x][1];
                                bValid = false;
                            }
                        }

                    }
                }
                if (fields.length &gt; 0) {
                   focusField.focus();
                   alert(fields.join('\n'));
                }
                return bValid;
            }]]&gt;
         &lt;/javascript&gt;

      &lt;/validator&gt;


      &lt;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"&gt;

         &lt;javascript&gt;&lt;![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 &gt;= 0) {
                                value = field.options[si].value;
                            }
                        } else {
                            value = field.value;
                        }

                        if (value.length &gt; 0) {

                            var iValue = parseInt(value);
                            if (isNaN(iValue) || !(iValue &gt;= -32768 &amp;&amp; iValue &lt;= 32767)) {
                                if (i == 0) {
                                    focusField = field;
                                }
                                fields[i++] = oShort[x][1];
                                bValid = false;
                           }
                       }
                    }
                }
                if (fields.length &gt; 0) {
                   focusField.focus();
                   alert(fields.join('\n'));
                }
                return bValid;
            }]]&gt;
         &lt;/javascript&gt;

      &lt;/validator&gt;


      &lt;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"&gt;

         &lt;javascript&gt;&lt;![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 &gt;= 0) {
                                value = field.options[si].value;
                            }
                        } else {
                            value = field.value;
                        }

                        if (value.length &gt; 0) {

                            if (!isAllDigits(value)) {
                                bValid = false;
                            } else {
                                var iValue = parseInt(value);
                                if (isNaN(iValue) || !(iValue &gt;= -2147483648 &amp;&amp; iValue &lt;= 2147483647)) {
                                    if (i == 0) {
                                        focusField = field;
                                    }
                                    fields[i++] = oInteger[x][1];
                                    bValid = false;
                               }
                           }
                       }
                    }
                }
                if (fields.length &gt; 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 &lt; argvalue.length; n++) {
                    if (validChars.indexOf(argvalue.substring(n, n+1)) == -1) return false;
                }
                return true;
            }]]&gt;
         &lt;/javascript&gt;

      &lt;/validator&gt;


      &lt;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"/&gt;


      &lt;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"&gt;

         &lt;javascript&gt;&lt;![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 &gt;= 0) {
                                value = field.options[si].value;
                            }
                        } else {
                            value = field.value;
                        }

                        if (value.length &gt; 0) {

                            var iValue = parseFloat(value);
                            if (isNaN(iValue)) {
                                if (i == 0) {
                                    focusField = field;
                                }
                                fields[i++] = oFloat[x][1];
                                bValid = false;
                            }
                        }
                    }
                }
                if (fields.length &gt; 0) {
                   focusField.focus();
                   alert(fields.join('\n'));
                }
                return bValid;
            }]]&gt;
         &lt;/javascript&gt;

      &lt;/validator&gt;


      &lt;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"/&gt;


      &lt;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"&gt;

         &lt;javascript&gt;&lt;![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') &amp;&amp;
                       (value.length &gt; 0) &amp;&amp;
                       (datePattern.length &gt; 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 &lt; orderYear &amp;&amp; orderDay &gt; 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 &amp;&amp; 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 &lt; orderYear &amp;&amp; orderMonth &gt; 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 &amp;&amp; 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 &gt; orderYear &amp;&amp; orderMonth &lt; 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 &amp;&amp; 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 &gt; 0) {
                  focusField.focus();
                  alert(fields.join('\n'));
               }
               return bValid;
            }

        function isValidDate(day, month, year) {
            if (month &lt; 1 || month &gt; 12) {
                    return false;
                }
                if (day &lt; 1 || day &gt; 31) {
                    return false;
                }
                if ((month == 4 || month == 6 || month == 9 || month == 11) &amp;&amp;
                    (day == 31)) {
                    return false;
                }
                if (month == 2) {
                    var leap = (year % 4 == 0 &amp;&amp;
                               (year % 100 != 0 || year % 400 == 0));
                    if (day&gt;29 || (day == 29 &amp;&amp; !leap)) {
                        return false;
                    }
                }
                return true;
            }]]&gt;
         &lt;/javascript&gt;

      &lt;/validator&gt;

&lt;!-- range is deprecated use intRange instead --&gt;
      &lt;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"&gt;

         &lt;javascript&gt;&lt;![CDATA[
            function validateRange(form) {
                return validateIntRange(form);
            }]]&gt;
         &lt;/javascript&gt;

      &lt;/validator&gt;

      &lt;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"&gt;

         &lt;javascript&gt;&lt;![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') &amp;&amp;
                        (form[oRange[x][0]].value.length &gt; 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 &gt;= iMin &amp;&amp; iValue &lt;= iMax)) {
                            if (i == 0) {
                                focusField = form[oRange[x][0]];
                            }
                            fields[i++] = oRange[x][1];
                            bValid = false;
                        }
                    }
                }
                if (fields.length &gt; 0) {
                    focusField.focus();
                    alert(fields.join('\n'));
                }
                return bValid;
            }]]&gt;
         &lt;/javascript&gt;

      &lt;/validator&gt;

      &lt;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"&gt;

         &lt;javascript&gt;&lt;![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') &amp;&amp;
                        (form[oRange[x][0]].value.length &gt; 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 &gt;= fMin &amp;&amp; fValue &lt;= fMax)) {
                            if (i == 0) {
                                focusField = form[oRange[x][0]];
                            }
                            fields[i++] = oRange[x][1];
                            bValid = false;
                        }
                    }
                }
                if (fields.length &gt; 0) {
                    focusField.focus();
                    alert(fields.join('\n'));
                }
                return bValid;
            }]]&gt;
         &lt;/javascript&gt;

      &lt;/validator&gt;

      &lt;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"&gt;

         &lt;javascript&gt;&lt;![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') &amp;&amp;
                        (form[oCreditCard[x][0]].value.length &gt; 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 &gt; 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 &amp; 1;
                    var sum = 0;
                    for (var count = 0; count &lt; no_digit; count++) {
                        var digit = parseInt(cardNumber.charAt(count));
                        if (!((count &amp; 1) ^ oddoeven)) {
                            digit *= 2;
                            if (digit &gt; 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 &lt; argvalue.length; n++) {
                    if ((argvalue.substring(n, n+1) &lt; "0") ||
                        (argvalue.substring(n,n+1) &gt; "9")) {
                        return false;
                    }
                }
                return true;
            }]]&gt;
         &lt;/javascript&gt;

      &lt;/validator&gt;


      &lt;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"&gt;

         &lt;javascript&gt;&lt;![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') &amp;&amp;
                        (form[oEmail[x][0]].value.length &gt; 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 &gt; 0) {
                    focusField.focus();
                    alert(fields.join('\n'));
                }
                return bValid;
            }

            /**
             * Reference: Sandeep V. Tamhankar (stamhankar@hotmail.com),
             * http://javascript.internet.com
             */
            function checkEmail(emailStr) {
               if (emailStr.length == 0) {
                   return true;
               }
               var emailPat=/^(.+)@(.+)$/;
               var specialChars="\\(\\)&lt;&gt;@,;:\\\\\\\"\\.\\[\\]";
               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 &lt;= 4; i++) {
                      if (IPArray[i] &gt; 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 &lt; 2) ||
                   (domArr[domArr.length-1].length &gt; 3)) {
                   return false;
               }
               if (len &lt; 2) {
                   return false;
               }
               return true;
            }]]&gt;
         &lt;/javascript&gt;

      &lt;/validator&gt;

   &lt;/global&gt;

&lt;/form-validation&gt;

web.xml

&lt;web-app&gt;
&lt;servlet&gt;
&lt;servlet-name&gt;AS&lt;/servlet-name&gt;
&lt;servlet-class&gt;org.apache.struts.action.ActionServlet&lt;/servlet-class&gt;
&lt;/servlet&gt;

&lt;servlet-mapping&gt;
&lt;servlet-name&gt;AS&lt;/servlet-name&gt;
&lt;url-pattern&gt;*.do&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;

&lt;welcome-file-list&gt;
&lt;welcome-file&gt;welcome.html&lt;/welcome-file&gt;
&lt;/welcome-file-list&gt;


  &lt;taglib&gt;
            &lt;taglib-uri&gt;/tags/struts-html&lt;/taglib-uri&gt;
            &lt;taglib-location&gt;/WEB-INF/struts-html.tld&lt;/taglib-location&gt;
      &lt;/taglib&gt;

      &lt;taglib&gt;
            &lt;taglib-uri&gt;/tags/struts-bean&lt;/taglib-uri&gt;
            &lt;taglib-location&gt;/WEB-INF/struts-bean.tld&lt;/taglib-location&gt;
      &lt;/taglib&gt;
&lt;/web-app&gt;



success.html
you have successfully regesterd

failure.html
try again

Applicationresources.Properties

regform.Sname=&lt;b&gt; Student Name&lt;/b&gt;
regform.Fname=&lt;b&gt; Father Name&lt;/b&gt;
regform.Occupation=&lt;b&gt;Occupation&lt;/b&gt;
regform.contactno=&lt;b&gt;Contact Number&lt;/b&gt;
regform.Email=&lt;b&gt;Email&lt;/b&gt;
regform.dob=&lt;b&gt;Date Of Birth&lt;/b&gt;
regform.identificationmarks=&lt;b&gt;Identification Marks&lt;/b&gt;
regform.lastschool=&lt;b&gt;Last School Studied&lt;/b&gt;
regform.lastclass=&lt;b&gt;Last Class&lt;/b&gt;
regform.marks=&lt;b&gt;Marks&lt;/b&gt;
regform.Percentage=&lt;b&gt;percentage&lt;/b&gt;
regform.EntranceTest=&lt;b&gt;Entrence Test&lt;/b&gt;
regform.percentageInTest=&lt;b&gt;Percentage In Test&lt;/b&gt;
regform.presentclass=&lt;b&gt;Present Class&lt;/b&gt;

errors.required=&lt;i&gt;&lt;li&gt;{0} field can no be empty&lt;/li&gt;&lt;/i&gt;&lt;br/&gt;
errors.integer=&lt;i&gt;&lt;li&gt;{0} must be an Integer&lt;/li&gt;&lt;/i&gt;&lt;br/&gt;
errors.email=&lt;i&gt;&lt;li&gt;{0} is an invalid email address&lt;/li&gt;&lt;/i&gt;&lt;br/&gt;
errors.date=&lt;i&gt;&lt;li&gt;{0} field can no be empty&lt;/li&gt;&lt;/i&gt;&lt;br/&gt;
errors.integer=&lt;i&gt;&lt;li&gt;{0} must be a number&lt;/li&gt;&lt;/i&gt;&lt;br/&gt;
errors.double=&lt;i&gt;&lt;li&gt;{0} must be a number&lt;/li&gt;&lt;/i&gt;&lt;br/&gt;

Advertisement
View Answers

Related Tutorials/Questions & Answers:
STRUTS
STRUTS  MAIN DIFFERENCES BETWEEN STRUTS 1 AND STRUTS 2
STRUTS
STRUTS  MAIN DIFFERENCES BETWEEN STRUTS 1 AND STRUTS 2
Advertisements
Struts
Struts   How to retrive data from database by using Struts
Struts
Struts  how to learn struts
Struts
Struts  what is SwitchAction in struts
STRUTS
STRUTS   Request context in struts? SendRedirect () and forward how to configure in struts-config.xml
struts
struts  in industry, struts 1 and struts 2. which is the best? which is useful as a professuional   Have a look at the following link: Struts Tutorials
struts
struts shopping cart project in struts with oracle database connection  shopping cart project in struts with oracle database connection   Have a look at the following link: Struts Shopping Cart using MySQL
struts
struts  what are the 4 methods of struts framework
Struts
Struts  Tell me good struts manual
Struts
Struts   When Submit a Form and while submit is working ,press the Refresh , what will happen in Struts
struts
struts  Hi what is struts flow of 1.2 version struts? i have struts applicatin then from jsp page how struts application flows Thanks Kalins Naik   Please visit the following link: Struts Tutorial
struts
struts  i have no any idea about struts.please tell me briefly about struts?**   Hi Friend, You can learn struts from the given link: Struts Tutorials Thanks
struts
struts  why doc type is not manditory in struts configuration file
struts
struts  why doc type is not manditory in struts configuration file
struts
struts  why doc type is not manditory in struts configuration file
struts
struts  why doc type is not manditory in struts configuration file
struts
struts   how to write Dao to select data from the database
Struts
Struts   What is called properties file in struts? How you call the properties message to the View (Front End) JSP Pages
Struts
Struts   in struts I want two struts.xml files. Where u can specify that xml files location and which tag u specified
Struts
Setter methods of form bean class in Struts applications  who calls the setter methods of form bean class in struts applications
struts
struts   Hi how struts flows from jsp page to databae and also using validation ? Thanks Kalins Naik
Struts
Struts  Hi i am new to struts. I don't know how to create struts please in eclipse help me
Struts
Struts  How Struts is useful in application development? Where to learn Struts? Thanks   Hi, Struts is very useful in writing web... performance Java web applications that runs on Java enabled application servers. Struts
struts
struts  which is the best book to study struts on own? please tell me   You can learn struts through the following books: 1)Programming Jakarta Struts By Chuck Cavaness 2)Professional Jakarta Struts By James Goodwill
struts
struts  which is the best book to study struts on own? please tell me   You can learn struts through the following books: 1)Programming Jakarta Struts By Chuck Cavaness 2)Professional Jakarta Struts By James Goodwill
struts
struts  how to start struts?   Hello Friend, Please visit the following links: http://www.roseindia.net/struts/ http://www.roseindia.net... can easily learn the struts. Thanks
Struts
Struts   1)in struts server side validations u are using programmatically validations and declarative validations? tell me which one is better ? 2) How to enable the validator plug-in file
Struts
Struts  Am newly developed struts applipcation,I want to know how to logout the page using the strus   Please visit the following link: Struts Login Logout Application
struts
struts  hi i would like to have a ready example of struts using "action class,DAO,and services" for understanding.so please guide for the same. thanks  Please visit the following link: Struts Tutorials
Struts - Struts
Struts for beginners  struts for beginners example
Struts
Struts   How will you retrieve a collection of records from business logic to presentation logic in framework
struts
struts  how to generate bar code for continuous numbers in a loop using struts ... or atleast for a given number
struts
struts  Hi,... please help me out how to store image in database(mysql) using struts
struts
struts  Hi, 1) can we write two controller classes in struts
struts
struts  please send me a program that clearly shows the use of struts with jsp
STRUTS
STRUTS  1)Have you used struts tag libraries in your application? 2)What are the various types of tag libraries in struts? Elaborate each of them? 3)How can you implement custom tag libraries in your application
STRUTS
STRUTS   1) Difference between Action form and DynaActionForm? 2) How the Client request was mapped to the Action file? Write the code and explain
STRUTS
STRUTS   Validation: user entered as 123.34567 I want the value as 123.34 once the user submits. How you will validate
struts
struts  hi i would like to have a ready example of struts using"action class,DAO,and services" so please help me
struts
struts  we have the concept of jsp's and servlets right we can develop the web-pages each and everything then why what for struts inturdouced
struts
struts  Hi, Here my quation is can i have more than one validation-rules.xml files in a struts application
struts
technologies like servlets, jsp,and struts. i am doing one struts application where i... the following links: http://www.roseindia.net/struts/StrutsCustomValidator.shtml http://www.roseindia.net/struts/addressstrutsvalidator.shtml http
Struts
Struts  What is Struts?   Hi hriends, Struts is a web page... web applications quickly and easily. Struts combines Java Servlets, Java Server... developers, and everyone between. Thanks.   Hi friends, Struts is a web
struts
struts   i have one textbox for date field.when i selected date from datecalendar then the corresponding date will appear in textbox.i want code for this in struts.plz help me
Struts
Struts  why in Struts ActionServlet made as a singleton what is the specific reason how it useful in the webapplication development?   Basically in Struts we have only Two types of Action classes. 1.BaseActions
struts - Struts
Struts s property tag  Struts s property tag
Struts - Struts
Struts examples for beginners  Struts tutorial and examples for beginners
Struts
Struts  Why struts rather than other frame works?   Struts is used into web based enterprise applications. Struts2 cab be used with Spring... by the application. There are several advantages of Struts that makes it popular
struts
struts  how to handle exception handling in struts reatime projects? can u plz send me one example how to deal with exception? can u plz send me how to develop our own exception handling
 

 

 

DMCA.com