Running jquery form validation and jquery post script form together

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

I run the post script when the submit button is clicked, so it takes effect before validation: / and I cannot change the validation to run before the post script :( so it now forms a submission without validation.

How to do it? It helps to appreciate!


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 the 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>  

      


The 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>

      

+2


source to share


1 answer


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

      

Call your ajax submit from the success function callback.



Check: http://docs.jquery.com/Plugins/Validation/validate for more information (I'm assuming we're talking about the same plugin).

+2


source







All Articles