struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Srinivas_Birag...@3com.com
Subject RE: JavaScript validation errors
Date Mon, 14 Aug 2006 09:39:03 GMT




Hi sean,

  i think if you put   onsubmit="return validateLoginForm(this); it works
fine with client(front-end validation)side and if you remove onsubmit from
jsp then it works fine with server(backend validation)....may be this helps
you

Thanks,
Srinivas.




                                                                           
             "O'Shea, Sean"                                                
             <Sean.O'Shea@fmr.                                             
             com>                                                       To 
                                       <user@struts.apache.org>            
             08/12/2006 12:11                                           cc 
             AM                                                            
                                                                   Subject 
                                       RE: JavaScript validation errors    
             Please respond to                                             
               "Struts Users                                               
               Mailing List"                                               
             <user@struts.apac                                             
                  he.org>                                                  
                                                                           
                                                                           




Hi,

I'm not writing the JavaScript code myself, so I cannot include a check
like you suggested. The JavaScript is being automatically generated
using the <html:javascript> tag. The generated validateRequired method
is taken directly from the commons-validator.jar:

    function validateRequired(form) {
        var isValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();
        var formName = form.getAttributeNode("name");

        oRequired = eval('new ' + formName.value + '_required()');

        for (x in oRequired) {
            var field = form[oRequired[x][0]];

            if ((field.type == 'hidden' ||
                field.type == 'text' ||
                field.type == 'textarea' ||
                field.type == 'file' ||
                field.type == 'checkbox' ||
                field.type == 'select-one' ||
                field.type == 'password') &&
                field.disabled == false) {

                var value = '';
                // get field's value
                if (field.type == "select-one") {
                    var si = field.selectedIndex;
                    if (si >= 0) {
                        value = field.options[si].value;
                    }
                } else if (field.type == 'checkbox') {
                    if (field.checked) {
                        value = field.value;
                    }
                } else {
                    value = field.value;
                }

                if (trim(value).length == 0) {

                    if (i == 0) {
                        focusField = field;
                    }
                    fields[i++] = oRequired[x][1];
                    isValid = false;
                }
            } else if (field.type == "select-multiple") {
                var numOptions = field.options.length;
                lastSelected=-1;
                for(loop=numOptions-1;loop>=0;loop--) {
                    if(field.options[loop].selected) {
                        lastSelected = loop;
                        value = field.options[loop].value;
                        break;
                    }
                }
                if(lastSelected < 0 || trim(value).length == 0) {
                    if(i == 0) {
                        focusField = field;
                    }
                    fields[i++] = oRequired[x][1];
                    isValid=false;
                }
            } else if ((field.length > 0) && (field[0].type == 'radio'
|| field[0].type == 'checkbox')) {
                isChecked=-1;
                for (loop=0;loop < field.length;loop++) {
                    if (field[loop].checked) {
                        isChecked=loop;
                        break; // only one needs to be checked
                    }
                }
                if (isChecked < 0) {
                    if (i == 0) {
                        focusField = field[0];
                    }
                    fields[i++] = oRequired[x][1];
                    isValid=false;
                }
            }
        }
        if (fields.length > 0) {
           focusField.focus();
           alert(fields.join('\n'));
        }
        return isValid;
    }

    // Trim whitespace from left and right sides of s.
    function trim(s) {
        return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
    }

Sean

-----Original Message-----
From: Monkeyden [mailto:monkeyden@gmail.com]
Sent: Friday, August 11, 2006 1:57 PM
To: Struts Users Mailing List
Subject: Re: JavaScript validation errors

Are you returning false when there is an error in the validation?

if(userName.trim().length() == 0){
    alert("Pathetic, technologically inept users must enter a user
name.");
    return false;
}


On 8/11/06, O'Shea, Sean <Sean.O'Shea@fmr.com> wrote:
>
> Hi Adam,
>
> I changed my login form to look like this:
>
> <html:form action="/login" method="POST" onsubmit="return
> validateLoginForm(this);">
> <table border="0" cellspacing="5">
>    <tr>
>      <th align="right">
>        <bean:message key="login.ssn"/>:
>      </th>
>      <td align="left">
>        <html:text property="SSN"/><bean:message
> key="login.correctssn"/>
>      </td>
>    </tr>
>    <tr>
>      <th align="right">
>        <bean:message key="login.password"/>:
>      </th>
>      <td align="left">
>        <html:password property="password"/><bean:message
> key="login.correctpw"/>
>      </td>
>    </tr>
>    <tr>
>      <td align="right">
>        <input type="submit" value="<bean:message
> key="login.button"/>"/>
>      </td>
>      <td align="left">
>        <input type="reset"/>
>      </td>
>    </tr>
> </table>
> </html:form>
>
> But still no luck. My server side validation still works fine, but the
> client side validation does not seem to execute.
> Even when I look at the source for my JSP I can see the call to the
> JavaScript function:
>
> <form name="loginForm" method="POST"
action="/MySampleApp/sample/login"
> onsubmit="return validateLoginForm(this);">
>
> Its as if the variables are not getting loaded into the JavaScript
> function, or the function is not getting called at all.
>
> Anyone have any ideas on this?
>
> Thanks again
>
> Sean
>
>
>
> -----Original Message-----
> From: Adam Gordon [mailto:adam.gordon@readytalk.com]
> Sent: Friday, August 11, 2006 11:56 AM
> To: Struts Users Mailing List
> Subject: Re: JavaScript validation errors
>
> Sean-
>
> You need to add an "onsubmit" attribute to your html:form" element
that
> calls the validation function.  Specifically, IIRC, it needs to say:
> onsubmit="return validateForm(this);
>
> happy coding,
>
> -adam
>
> O'Shea, Sean wrote:
> > Hi all,
> >
> > I'm using struts 1.2.7 with commons-validator-1.1.4 and I'm running
> into
> > a few generated JavaScript errors. Here's what my JSP looks like:
> >
> > <html:javascript formName="loginForm" />
> > <html:form action="/login" method="POST">
> >   <table border="0" cellspacing="5">
> >     <tr>
> >       <th align="right">
> >               <bean:message key="login.ssn"/>:
> >       </th>
> >       <td align="left">
> >               <html:text property="SSN"/><bean:message
> > key="login.correctssn"/>
> >       </td>
> >     </tr>
> >     <tr>
> >       <th align="right">
> >               <bean:message key="login.password"/>:
> >       </th>
> >       <td align="left">
> >               <html:password property="password"/><bean:message
> > key="login.correctpw"/>
> >       </td>
> >     </tr>
> >     <tr>
> >       <td align="right">
> >               <input type="submit" value="<bean:message
> > key="login.button"/>"/>
> >       </td>
> >       <td align="left">
> >               <input type="reset"/>
> >       </td>
> >     </tr>
> >   </table>
> > </html:form>
> >
> > Here's what my action mapping looks like:
> >
> >               <action path="/login" name="loginForm"
> > type="LoginAction"
> >                               scope="request" validate="true"
> > input="login">
> > .....
> >               </action>
> >
> > This is what my form bean looks like:
> >
> >               <form-bean name="loginForm"
> > type="org.apache.struts.validator.DynaValidatorForm">
> >                       <form-property name="SSN"
> > type="java.lang.String" />
> >                       <form-property name="password"
> > type="java.lang.String" />
> >               </form-bean>
> >
> > My form validations look like this:
> >
> >               <form name="loginForm">
> >                       <field property="SSN" depends="required">
> >                               <arg key="login.ssn" position="0"/>
> >                       </field>
> >                       <field property="password" depends="required">
> >                               <arg key="login.password"
position="0"/>
> >                       </field>
> >               </form>
> >
> > When I try to submit my loginForm. my server die validations work
> fine,
> > but the JavaScript does not get executed. From looking at the
> generated
> > source I see this:
> >
> > <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("SSN", "SSN is required.", new Function
> > ("varName", " return this[varName];"));
> >      this.a1 = new Array("password", "Password is required.", new
> > Function ("varName", " return this[varName];"));
> >     }
> >
> > Either the JavaScript is not getting called, or the generated
> JavaScript
> > does not match up. Could it be something to do with the
> html:javasctipt
> > tag? Looking at the source I have for the JavascriptValidatorTag
class
> I
> > see this version:
> >
> >  * $Id: JavascriptValidatorTag.java 165208 2005-04-28 21:41:45Z
mrdon
> $
> >
> > All help would be greatly appreciated.
> >
> > Thanks
> >
> > Sean
> >
> >
---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


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



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


Mime
View raw message