views:

794

answers:

1

A form, I use AJAX to transfer the data. The AJAX is triggered by onClick="", I also use JQuery validate(). JQuery validate() is executed after the onclick=“function()", I would like the JQuery validate() is executed before the onclick="functionname()", how to do it?

<?php $addtext=<<<html
<a id="give_comment" href="javascript:void(0)">Supplement</a>
<div id="comment_text" name="comment_text" style="display:none">
<form id="add_text" action="nofile.php"  method="post" onsubmit="return false;">
<textarea  id="giver_comment_text" name="giver_comment_text" rows="6" cols="70" class="font1"></textarea><br/>
<input id="giver_submit_comment" type="submit" value="Submit" onclick="validateDetail()" /></form></div>
<br/>
html;
echo $addtext;
?>

Javascript code:

function validateDetail()
{
  $('#add_text').validate({
        rules:{
         giver_comment_text:{
          required:true,
          minlength:50,
                  maxlength:600 
         }
        },
      });
    var detail=$('#giver_comment_text').val();

//$.post(The content of AJAX);
}
+1  A: 

You can remove the function from the onClick handler, and use a submitHandler. Read the documentation, and here's the sample they provide there:

$(".selector").validate({   
  submitHandler: function(form) {
  // do other stuff for a valid form
  form.submit();    
  } 
})

So all you have to do is call function() inside the submitHandler after the form has been validated.

Traveling Tech Guy
Can you be more specific? Where to place ('#add_text').validate({ rules:{ giver_comment_text:{ required:true, minlength:50, maxlength:600 } }, });
Steven
`submitHandler` is one of the options to the `validate` function. Just look at the samples provided at the link.
Traveling Tech Guy