You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Kiru <ki...@gmail.com> on 2009/04/28 16:46:01 UTC

Validating Struts2 fields using JQuery

I am using JQuery for validating the fields in my jsp page, created using
Struts 2.1.6. But the jquery validation is
not firing and the form is getting submitted. In the same page I have
created tabs, date picker using JQuery and
they are all working fine. The same JQuery validtion is working fine with
pure html (no struts tag) form. The fields
 that i am trying to validate are inside the jquery tab. Any idea what could
be missing/wrong?

Thanks.

Here is portion of my  .jsp
<%@ taglib prefix="s" uri="/struts-tags"%>
<link type="text/css" href="/abc/css/jquery-ui-1.7.1.custom.css"
rel="stylesheet" />
<script type="text/javascript" src="/abc/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="
http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js
"></script>
<script type="text/javascript"
src="/abc/js/jquery-ui-1.7.1.custom.min.js"></script>
<link rel="stylesheet" type="text/css" href="../css/validate.css" />

<script type="text/javascript">
$(document).ready(function() {
  // validate signup form
   $("#myForm").validate({
   rules: {
    myForm_userId: "required",
   },
   messages: {
    myForm_userId: "Enter user ID",

   },
   // the errorPlacement has to take the table layout into account
    errorPlacement: function(error, element) {
    if ( element.is(":radio") )
     error.appendTo( element.parent().next().next() );
    else if ( element.is(":checkbox") )
     error.appendTo ( element.next() );
    else
     error.appendTo( element.parent().next() );
   },
   // specifying a submitHandler prevents the default submit, good for the
demo
                    /*  submitHandler: function() {
     alert("submitted!");
   }, */
   success: function(label) {
    // set &nbsp; as text for IE
    label.html("&nbsp;").addClass("checked");
   }
  });

 });
 </script>

 <s:form name="myForm" id="myForm" action="myResults"
 namespace="/" method="post" theme="xhtml">

 <table align="center" border="0">
 <table align="center">

     <tr>
      <td colspan="2">
      <s:fielderror cssClass="error">
       <s:param value="userID" />
      </s:fielderror>
      </td>
     </tr>
     <s:textfield name="userID"
      label="%{getText('login.label.userID')}" required="true"
      requiredposition="left" maxlength="10">
      <s:param name="labelstyle" value="%{'boldtext'}" />
     </s:textfield>
    </table>

</s:form>

Re: Validating Struts2 fields using JQuery

Posted by dusty <du...@yahoo.com>.
I can't think of anything with a quick breeze through of your message, but
you should use the id parameter of the s:textfield tag so you know what the
HTML id will be in your $("#...") call.



Kiru-2 wrote:
> 
> I am using JQuery for validating the fields in my jsp page, created using
> Struts 2.1.6. But the jquery validation is
> not firing and the form is getting submitted. In the same page I have
> created tabs, date picker using JQuery and
> they are all working fine. The same JQuery validtion is working fine with
> pure html (no struts tag) form. The fields
>  that i am trying to validate are inside the jquery tab. Any idea what
> could
> be missing/wrong?
> 
> Thanks.
> 
> Here is portion of my  .jsp
> <%@ taglib prefix="s" uri="/struts-tags"%>
> <link type="text/css" href="/abc/css/jquery-ui-1.7.1.custom.css"
> rel="stylesheet" />
> <script type="text/javascript" src="/abc/js/jquery-1.3.2.min.js"></script>
> <script type="text/javascript" src="
> http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js
> "></script>
> <script type="text/javascript"
> src="/abc/js/jquery-ui-1.7.1.custom.min.js"></script>
> <link rel="stylesheet" type="text/css" href="../css/validate.css" />
> 
> <script type="text/javascript">
> $(document).ready(function() {
>   // validate signup form
>    $("#myForm").validate({
>    rules: {
>     myForm_userId: "required",
>    },
>    messages: {
>     myForm_userId: "Enter user ID",
> 
>    },
>    // the errorPlacement has to take the table layout into account
>     errorPlacement: function(error, element) {
>     if ( element.is(":radio") )
>      error.appendTo( element.parent().next().next() );
>     else if ( element.is(":checkbox") )
>      error.appendTo ( element.next() );
>     else
>      error.appendTo( element.parent().next() );
>    },
>    // specifying a submitHandler prevents the default submit, good for the
> demo
>                     /*  submitHandler: function() {
>      alert("submitted!");
>    }, */
>    success: function(label) {
>     // set &nbsp; as text for IE
>     label.html("&nbsp;").addClass("checked");
>    }
>   });
> 
>  });
>  </script>
> 
>  <s:form name="myForm" id="myForm" action="myResults"
>  namespace="/" method="post" theme="xhtml">
> 
>  <table align="center" border="0">
>  <table align="center">
> 
>      <tr>
>       <td colspan="2">
>       <s:fielderror cssClass="error">
>        <s:param value="userID" />
>       </s:fielderror>
>       </td>
>      </tr>
>      <s:textfield name="userID"
>       label="%{getText('login.label.userID')}" required="true"
>       requiredposition="left" maxlength="10">
>       <s:param name="labelstyle" value="%{'boldtext'}" />
>      </s:textfield>
>     </table>
> 
> </s:form>
> 
> 

-- 
View this message in context: http://www.nabble.com/Validating-Struts2-fields-using-JQuery-tp23278971p23290352.html
Sent from the Struts - User mailing list archive at Nabble.com.


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