Client Side Validation with xVal for Forms Submitted via Ajax

by Bill Beckelman 27. November 2009 18:03

I had a really hard time figuring out how to use xVal with forms submitted via Ajax and it took a while on Google to try and figure it. All you have to do is set a submitHandler for the form to use. From the jQuery Validate documentation a submitHandler is a:

“Callback for handling the actual submit when the form is valid. Gets the form as the only argument. Replaces the default submit. The right place to submit a form via Ajax after it validated.”

Code Example:

Code Snippet
  1. $("#FormId").data("validator").settings.submitHandler = function(form) {
  2.     $.ajax({
  3.         url: 'your_url',
  4.         type: 'POST',
  5.         data: $(form).serialize(),
  6.         success: function(data) {                        
  7.             document.forms['FormName'].reset();
  8.         }
  9.     });
  10. }

Tags:

jQuery | xVal

Comments


About Me

I live and work in Salt Lake City, Utah. My background is in aviation. I have a degree in Aeronautical Science from Embry-Riddle Aeronautical University in Prescott, AZ. I have worked as a commercial airline pilot and most recently as a technical advisor for a charter airline.