Running jquery form validation and jquery form post script together

Question!

I have 2 different scripts, one is jquery post for form, and second is jquery form validation.

I run post script at submit button click action, so it takes effect before validation :/ and i could not change validation to run before post script :( so now it form submitting without validating.

How can make it? Appreciate helps!


JQuery POST code

$(function() {

  $("#btnsend").click(function() {

    	var dataString = 'fullname='+ escape(document.contact.full_name.value);
    	$.ajax({
    	  type: "POST",
    	  url: "query.php?act=sendMail",
    	  data: dataString,
    	  success: function() {

          	$('#contact-form').hide()
    				  .html("<p>thanks!</p>")
            		          .fadeIn(500, function() {$('#contact-form').append("");});
    	   } 

        });

        return false;
    });

});


JQuery Validation

thanks for plugin http://bassistance.de/jquery-plugins/jquery-plugin-validation/

<!-- Form Validation -->
<script src="inc/validation/jquery.validate.js" type="text/javascript"></script>
<script src="inc/validation/js/cmxforms.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="inc/validation/css/validation.css" media="screen" />

<script type="text/javascript">
$(document).ready(function() {
	$("#commentForm").validate();
});
</script>


Form

<form id="commentForm" name="contact" method="post" action="">
    <ul id="contact-form">
        <li><label for="full_name">Full Name: *</label><input type="text" name="full_name" id="full_name" class="txt_input required" /></li>
        <li class="alignRight"><input type="submit" value="Send" id="btnsend" name="btnsend" class="btn_submit" /></li>
    </ul>
</form>


Answers
$("#commentForm").validate( {
   submitHandler: function(form) {
      // validation success, do something
   }
});

Call your ajax submit from within the success function callback.

Check: http://docs.jquery.com/Plugins/Validation/validate for more info (I assume we are talking about the same plugin).



This video can help you solving your question :)
By: admin