Re-enable jQuery validation after disabled

372 views Asked by At

I have an MVC project where I am using the built in validation using data annotations with jquery.validate.js and jquery.validate.unobtrusive.js. This all works fine and my form will validate.

On one form I have a postcode lookup button so I have disabled the validation for this button by using the suggestions by adding the formnovalidate attribute to the button. This also works well and allows me to submit the form to do my postcode lookup using ajax.

After I have clicked this button, I can then click on the main submit button and it will post the form without any validation happening.

I have had a look in the jquery.validate.js file and have seen that the validation is cancelled using this code:

// allow suppressing validation by adding the html5 formnovalidate attribute to the submit button
if ( $(event.target).attr("formnovalidate") !== undefined ) {
    validator.cancelSubmit = true;
}

I can then re-enable it by adding this after:

if ( event.target.id == 'submit' ) {
    validator.cancelSubmit = false;
}

However I don't want to change the source file as if I do an update using nuget I may forget that I have altered that file and forget to put that line back in.

How would I bind the validator.cancelSubmit = false; to the button click - something like this (which won't work as I don't have the validator object):

$('#submit').on('click', function() {
    validator.cancelSubmit = false;
});

I have tried using $.validator.cancelSubmit = false; but this didn't work

1

There are 1 answers

1
haim770 On BEST ANSWER

You can access the validator object using data():

$(yourForm).data('validator').cancelSubmit = false;