struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Kote, Nijaguna Sangankal" <nijaguna-sangankal.k...@hp.com>
Subject RE: Javascript error in struts validator.
Date Fri, 02 Jul 2004 08:09:16 GMT
I found why is it giving javascript error...
Because, when struts generates javascript code in jsp... Instead of
generating required() method it generates xForm_required() method.
This method is internally called by validateRequired() method.

Attached is the generated struts javascript code 

<script type="text/javascript" language="Javascript1.1"> 
<!-- Begin 

     var bCancel = false; 

    function validateLoginForm(form) {

        if (bCancel) 
      return true; 
        else 
 var formValidationResult;
       formValidationResult = validateRequired(form); 
     return (formValidationResult == 1);
   } 

    function loginForm_required () { 
     this.a0 = new Array("j_username", "null is required.", new Function
("varName", " return this[varName];"));
     this.a1 = new Array("j_password", "null is required.", new Function
("varName", " return this[varName];"));
    } 



function validateFloat18(form) {
                var bValid = true;
                var focusField = null;
                var i = 0;
                var fields = new Array();
                oFloat = new FloatValidations18();
                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 = trim( field.options[si].value );
                            }
                        } else {
                            value = field.value;
                            value = trim(value);
                             
                        }
                        if( isValidDecimal( value ) )
                        {
                            if ( value.length > 0   ) 
                            {
                                // remove '.' before checking digits
                                var tempArray = value.split('.');
                                var joinedString= tempArray.join('');
    
                                if (!isAllDigits(joinedString)) 
                                {
                                    bValid = false;
                                    if (i == 0) {
                                        focusField = field;
                                    }
                                    fields[i++] = oFloat[x][1];
    
                                } 
                                else 
                                {
                                    
                                    var iValue = parseFloat(value);
                                   
                                    if (isNaN(iValue)|| !(iValue >= 0 &&
iValue < 1000000000000000 )) 
                                    {
                                        if (i == 0) 
                                        {
                                            focusField = field;
                                        }
                                        fields[i++] = oFloat[x][1];
                                        bValid = false;
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (i == 0) 
                            {
                                focusField = field;
                            }
                            fields[i++] = oFloat[x][1];
                            bValid = false;
                        }
                    }
                }
                if (fields.length > 0) {
                   focusField.focus();
                   if (focusField.type == 'text' ||
                        focusField.type == 'textarea' ||
                        focusField.type == 'password') 
                   {
                       focusField.select();
                   }
                   alert(fields.join('\n'));
                }
                return bValid;
            }
            function isValidDecimal(val)
            {
                
                var i = val.indexOf(".");
                if ( i == -1 )
                {
                   return true;
                 }
                var afterDecimal = val.substring(i+1);
                if( isAllDigits(afterDecimal) && ( afterDecimal.length
<= 3 ) )
                {
                    return true;
                }
                
                return false;
             }
function validateMaxLength(form) {
                var isValid = true;
                var focusField = null;
                var i = 0;
                var fields = new Array();
                oMaxLength = new maxlength();
                for (x in oMaxLength) {
                    var field = form[oMaxLength[x][0]];

                    if (field.type == 'text' ||
                        field.type == 'textarea') {

                        var iMax =
parseInt(oMaxLength[x][2]("maxlength"));
                        if (field.value.length > iMax) {
                            if (i == 0) {
                                focusField = field;
                            }
                            fields[i++] = oMaxLength[x][1];
                            isValid = false;
                        }
                    }
                }
                if (fields.length > 0) {
                   focusField.focus();
                   if (focusField.type == 'text' ||
                        focusField.type == 'textarea' ||
                        focusField.type == 'password') 
                   {
                       focusField.select();
                   }
                   alert(fields.join('\n'));
                }
                return isValid;
            }
function validateRequired(form) {
                var isValid = 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 (trim(value).length == 0) {

                            if (i == 0) {
                                focusField = field;
                            }
                            fields[i++] = oRequired[x][1];
                            isValid = false;
                        }
                    }
                }
                if (fields.length > 0) {
                   focusField.focus();
                   if (focusField.type == 'text' ||
                        focusField.type == 'textarea' ||
                        focusField.type == 'password') 
                   {
                       focusField.select();
                   }
                   alert(fields.join('\n'));
                }
                return isValid;
            }

            // Trim whitespace from left and right sides of s.
            function trim(s) {
                return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
            }
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;
                                value = trim(value);
                            }
                        } else {
                            value = field.value;
                            value = trim(value);
                        }
                        if (value.length > 0) {
                            if (!isAllDigits(value)) {
                                bValid = false;
                                if (i == 0) {
                                    focusField = field;
                                }
                                fields[i++] = oInteger[x][1];
                            } 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();
                   if (focusField.type == 'text' ||
                        focusField.type == 'textarea' ||
                        focusField.type == 'password') 
                   {
                       focusField.select();
                   }
                   alert(fields.join('\n'));
                }
                return bValid;
            }

            function isAllDigits(argvalue) {
                argvalue = argvalue.toString();
                var validChars = "0123456789";
                var startFrom = 0;
                if (argvalue.charAt(0) == "-") {
                    startFrom = 1;
                }

                for (var n = startFrom; n < argvalue.length; n++) {
                    if (validChars.indexOf(argvalue.substring(n, n+1))
== -1) return false;
                }
                return true;
            }
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 = trim( field.options[si].value );
                            }
                        } else {
                            value = field.value;
                            value = trim(value);
                        }
                        if (value.length > 0) {
                            // remove '.' before checking digits
                            var tempArray = value.split('.');
                            var joinedString= tempArray.join('');

                            if (!isAllDigits(joinedString)) {
                                bValid = false;
                                if (i == 0) {
                                    focusField = field;
                                }
                                fields[i++] = oFloat[x][1];

                            } else {
                                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();
                   if (focusField.type == 'text' ||
                        focusField.type == 'textarea' ||
                        focusField.type == 'password') 
                   {
                       focusField.select();
                   }
                   alert(fields.join('\n'));
                }
                return bValid;
            }
function validateFloat15(form) {
                var bValid = true;
                var focusField = null;
                var i = 0;
                var fields = new Array();
                oFloat = new FloatValidations15();
                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 = trim( field.options[si].value );
                            }
                        } 
                        else 
                        {
                            value = field.value;
                            value = trim(value);
                        }
                        if( isValidDecimal( value ) )
                        {
                            if ( value.length > 0  ) 
                            {
                                // remove '.' before checking digits
                                var tempArray = value.split('.');
                                var joinedString= tempArray.join('');
    
                                if (!isAllDigits(joinedString)) 
                                {
                                    bValid = false;
                                    if (i == 0) 
                                    {
                                        focusField = field;
                                    }
                                    fields[i++] = oFloat[x][1];
    
                                } 
                                else 
                                {
                                    var iValue = parseFloat(value);
                                    if (isNaN(iValue)|| !(iValue >= 0 &&
iValue < 1000000000000 ) ) {
                                        if (i == 0) {
                                            focusField = field;
                                        }
                                        fields[i++] = oFloat[x][1];
                                        bValid = false;
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (i == 0) 
                            {
                                focusField = field;
                            }
                            fields[i++] = oFloat[x][1];
                            bValid = false;
                        }
                    }
                }
                if (fields.length > 0) {
                   focusField.focus();
                   if (focusField.type == 'text' ||
                        focusField.type == 'textarea' ||
                        focusField.type == 'password') 
                   {
                       focusField.select();
                   }
                   alert(fields.join('\n'));
                }
                return bValid;
            }
function validateMask(form) {
                var isValid = true;
                var focusField = null;
                var i = 0;
                var fields = new Array();
                oMasked = new mask();
                for (x in oMasked) {
                    var field = form[oMasked[x][0]];

                    if ((field.type == 'text' ||
                         field.type == 'textarea') &&
                         (field.value.length > 0)) {

                        if (!matchPattern(trim(field.value),
oMasked[x][2]("mask"))) {
                            if (i == 0) {
                                focusField = field;
                            }
                            fields[i++] = oMasked[x][1];
                            isValid = false;
                        }
                    }
                }

                if (fields.length > 0) {
                   focusField.focus();
                   if (focusField.type == 'text' ||
                        focusField.type == 'textarea' ||
                        focusField.type == 'password') 
                   {
                       focusField.select();
                   }
                   alert(fields.join('\n'));
                }
                return isValid;
            }

            function matchPattern(value, mask) {
               return mask.exec(value);
            }
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();
                   if (focusField.type == 'text' ||
                        focusField.type == 'textarea' ||
                        focusField.type == 'password') 
                   {
                       focusField.select();
                   }
                  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;
            }
function validateMinLength(form) {
                var isValid = true;
                var focusField = null;
                var i = 0;
                var fields = new Array();
                oMinLength = new minlength();
                for (x in oMinLength) {
                    var field = form[oMinLength[x][0]];

                    if (field.type == 'text' ||
                        field.type == 'textarea') {

                        var iMin =
parseInt(oMinLength[x][2]("minlength"));
                        if ((trim(field.value).length > 0) &&
(field.value.length < iMin)) {
                            if (i == 0) {
                                focusField = field;
                            }
                            fields[i++] = oMinLength[x][1];
                            isValid = false;
                        }
                    }
                }
                if (fields.length > 0) {
                   focusField.focus();
                   if (focusField.type == 'text' ||
                        focusField.type == 'textarea' ||
                        focusField.type == 'password') 
                   {
                       focusField.select();
                   }
                   alert(fields.join('\n'));
                }
                return isValid;
            }
function validateLong(form) {
                var bValid = true;
                var focusField = null;
                var i = 0;
                var fields = new Array();
                oLong = new LongValidations();
                for (x in oLong) {
                    var field = form[oLong[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 = trim( field.options[si].value );
                            }
                        } else {
                            value = trim( field.value );
                        }

                        if (value.length > 0) {

                            if (!isAllDigits(value)) {
                                bValid = false;
                                if (i == 0) {
                                    focusField = field;
                                }
                                fields[i++] = oLong[x][1];

                            } else {
                                var longValue = parseFloat(value);
                                if (isNaN(longValue) || !(longValue >= 0
&& longValue <= 9223372036854775807)) {
                                    if (i == 0) {
                                        focusField = field;
                                    }
                                    fields[i++] = oLong[x][1];
                                    bValid = false;
                               }
                           }
                       }
                    }
                }
                if (fields.length > 0) {
                   focusField.focus();
                   if (focusField.type == 'text' ||
                        focusField.type == 'textarea' ||
                        focusField.type == 'password') 
                   {
                       focusField.select();
                   }
                   alert(fields.join('\n'));
                }
                return bValid;
            }

//End --> 
<script>

Now problem is, how do I make struts generate required() method instead
of xForm_required() method.
I am using struts 1.1.


Thanks 
Guna

-----Original Message-----
From: Kote, Nijaguna Sangankal 
Sent: Friday, July 02, 2004 12:08 PM
To: user@struts.apache.org
Subject: Javascript error in struts validator.

Hi All,
	I had posted this question, but got now answer. Please give me
some hints on this.

	I am getting javascript error when I click on submit to validate
my form.
	It's a simple login page with username and password.

	here is the code for login.jsp

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib
uri="/WEB-INF/struts-html.tld" prefix="html" %>

<%
    response.setHeader("pragma", "no-cache");
    response.setHeader("Cache-control", "no-cache, no-store");
    response.setHeader("Expires", "0");
%>

<html:html locale="true">

<head>
	<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
	<link href="../css/asianet.css" type=text/css rel=stylesheet>
	<title><bean:message key="title.login"/></title>
	<html:javascript formName="loginForm"/> - This is how I am
including the Javascript for validation.
</head>
........................................... Rest body code goes here.
</html:html>

This is my validation.xml file
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons Validator Rules
Configuration 1.1//EN"
          "http://jakarta.apache.org/commons/dtds/validator_1_1.dtd">

<form-validation>

    <formset>

        <form    name="loginForm">
            <field     property="j_username"
                        depends="required">
                <arg0 key="prompt.userid"/>
            </field>

            <field     property="j_password"
                        depends="required">
                   <arg0 key="prompt.password"/>
            </field>

        </form>
    </formset>

</form-validation>

And validation-rules.xml file
<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 isValid = true;
                var focusField = null;
                var i = 0;
                var fields = new Array();
                oRequired = new required();  //-> here is the error.
                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') {
                        .............................. Rest

Now when I load my jsp, struts generates javascript with
validateLoginForm( document.loginForm )mehtod.
But, when I click on submit button. I get javascript error saying
"required is undefined" and it points to the above bold line in
Validation-rules.xml file.

I am not able to figure out why, any ideas or hints on this would help
me a lot.

Thanks
Guna


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message