2

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);
}
2
  • 1
    jQuery.validate attaches itself to the onsubmit event of the form. So validation is performed just before submitting. You will need to show your code to better understand the scenario. Commented Nov 4, 2009 at 7:31
  • +1 a sample would be great ... Commented Nov 4, 2009 at 7:34

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.