0

I want to have jQuery validation when the whole form is completed and submitted.

But when a drop down list is changed, I only want to have a POST to action so that I can get a refreshed view with updates. Say, when a state is chosen, the form post to the server side with current half-finished data, and the server returned the same page but with facilities specific to that selected state.

In this case, I want to submit the form without validation.

Like this:

$(document).on('change', '#selectStates', function(event) {
    event.preventDefault();
    
    var $form = $('#formEdit');
    
    $form.bypassValidate();
    $form.submit();
}); 

So what is the right way to $form.bypassValidate();?

We know in submit buttons, we can add a formnovalidate tag to bypass validation. But how can we do this in JavaScript?


Update with my solution

Okay, I looked through the post jQuery Validation plugin: disable validation for specified submit buttons again. And alas, there is a solution hidden in one of the answers.

formnovalidate does not apply here if I need to bypass validation in JavaScript.

All we need is to call cancelSubmit (which should be named cancelValidate IMHO) on the validator, which is bound to our jQuery form.

So my code on the select input is:

$(document).on('change', '#selectStates', function(event) {
    event.preventDefault();
    
    var $form = $('#formEdit');

    $form.data('validator').cancelSubmit = true;
    $form.submit();
}); 
Community
  • 1
  • 1
Blaise
  • 21,314
  • 28
  • 108
  • 169
  • This seems like a misuse of forms. Can you separate out the select into its own form that does what you want or use an ajax call to dynamically change the page contents? – Brian Campbell May 24 '16 at 21:13
  • *"Is there something like a `formnovalidate` tag, which can be placed on a button to submit the form without validation?"* Exactly: http://stackoverflow.com/a/203989/594235 – Sparky May 24 '16 at 22:15
  • Sparky, I won't say it is a duplicate question. Nowhere in that SO question did I find how to bypass validation when submitting the form in JavaScript. We could not use "formnovalidate" on the drop down list. – Blaise May 24 '16 at 22:36

0 Answers0