2

I'm using the jQuery.validate() to validate my form before it is submitted. Now that I am using Ajax to submit my form, I have a problem.

Note: I am not using the jQuery form plugin. Just so that is clear :)

The jQuery.validate() is fired, but the form submission is also executed.

I don't want the form to be submitted until the validator is ok. Any god suggestions on how I can accomplish this?

UPDATE

I found the answer. I had to use the submitHandler. Now my form is only submitted if all the required fields are corectly populated.

and here's how:

if( jQuery('#my_form').length > 0 ) {
  jQuery("#my_form").validate({
     rules: {
       brand_name: "required",
       brand_email: {email: true },
       brand_description: "required"
     },
       submitHandler: function() { processRegistration(jQuery("#my_form").attr("id"), jQuery("#my_form").serialize());  }
  });  


} 
Steven
  • 19,224
  • 47
  • 152
  • 257

1 Answers1

1
jQuery('#registrationforms form').submit(function() {
  if (processRegistration(jQuery(this).attr("id"), jQuery(this).serialize())) {
    // do ajax thing
  };
  return false;
});
Eimantas
  • 48,927
  • 17
  • 132
  • 168
  • So I should put my jQuery("#store_data_form").validate() inside this? Because that's not working at all. – Steven Sep 25 '09 at 09:03
  • What does processRegistration() return? It should return validate()'s result (i presume true or false). – Eimantas Sep 25 '09 at 09:15
  • You can see the processRegistration() her: http://stackoverflow.com/questions/1468605/jquery-dialog-box-not-opening-2nd-time – Steven Sep 25 '09 at 09:32
  • I don't have time now, but I will try something simialr to: $("#myform").submit(function(event) { handleSubmit(event.target); }).validate(); – Steven Sep 25 '09 at 09:34