views:

313

answers:

1

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());  }
  });  


}
+1  A: 
jQuery('#registrationforms form').submit(function() {
  if (processRegistration(jQuery(this).attr("id"), jQuery(this).serialize())) {
    // do ajax thing
  };
  return false;
});
Eimantas
So I should put my jQuery("#store_data_form").validate() inside this? Because that's not working at all.
Steven
What does processRegistration() return? It should return validate()'s result (i presume true or false).
Eimantas
You can see the processRegistration() her: http://stackoverflow.com/questions/1468605/jquery-dialog-box-not-opening-2nd-time
Steven
I don't have time now, but I will try something simialr to: $("#myform").submit(function(event) { handleSubmit(event.target);}).validate();
Steven
I found the answer.
Steven
And what was that answer?
El Yobo