views:

707

answers:

1

Hi friends,

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>
+1  A: 
$("#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).

Sbm007
I tired to put $("#btnsend").click(function() { ...... all codehere ....... } to the place where you have noted //validation success.it run validation first, thats fine, but it is not running the jquery-post aftet that :/ I guess I should use something else than .click() for there...
artmania
ps I use http://bassistance.de/jquery-plugins/jquery-plugin-validation/ for validation
artmania
Unfortunately there doesn't seem to be an easy solution, I suggest you add an if check before the ajax to confirm whether the field has been filled in before sending the request. If you only have one input field this should be okay, but if you have more and would like to use the validation plugin then you need a different solution. I'm sorry I can't help you on that.
Sbm007