Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery validate submitHandler triggers validation

When using the jQuery submitHandler method, I'm posting the form data via ajax to the server. On success, another method is called that resizes a popup and notifies the user its all complete.

My problem is however, whilst posting the data, the form is getting validated again causing undesired effects.

My understanding is submitHandler shouldn't trigger the validation again?

submitHandler: function(form) {
    console.log('test');
}

In this instance, 'test' is being logged to the console, but the validation is being run once again (in this case, a server side validity check on an email address field).

I'm using a button type submit as the form element. Do I need to prevent defaults or something similar?

like image 535
james Avatar asked Sep 28 '11 10:09

james


People also ask

How do you check if jQuery validate is working?

fn , so simply check whether it exists or not; if (typeof jQuery. fn. validationPlugin === "function") { // It's loaded } else { // It's not. }

Does jQuery validate require a form?

The jquery validate plugin requires a form element to function, so you should have your form fields (no matter how few) contained inside a form. You can tell the validation plugin not to operate on form submission, then manually validate the form when the correct submit button is clicked.

What is Errorplacement?

Definition of jQuery validate errorplacement. The jQuery validate errorplacement() function is used to customize the placement of an error message. This function is a built-in function in jQuery. These function changes the default behavior of the validation plugin to insert the error label after the input fields.

What is jQuery validate unobtrusive?

An unobtrusive validation in jQuery is a set of ASP.Net MVC HTML helper extensions.By using jQuery Validation data attributes along with HTML 5 data attributes, you can perform validation to the client-side.


1 Answers

Your submitHandler should exist within the validate function. In addition you should call form.submit() yourself which will remove the recursive calls that would otherwise exist.

     $("#myForm").validate({
          submitHandler: function (form) {
              console.log('test');
              form.submit();
          }
     });
like image 63
Aaron McIver Avatar answered Sep 25 '22 01:09

Aaron McIver