Disable Enable unobtrusive verification for a specific submit button.

I have two buttons to send Back, Continue. What do I need to do to disable client verification when I click "Back". I tried to add a cancel class to the button attribute, but that does not help.

UPD This actually works to undo the class . But it does not work if you add it dynamically (by javascript).

+6
source share
4 answers

I bound the event handler to specific buttons that changed the settings of the validator object in this particular form.

$(".jsCancel").click(function (e) { $(e.currentTarget).closest("form").validate().settings.ignore = "*" }); 

It worked like a charm for me in MVC3.

I don’t know if this helps you in particular, but since I use an ajax form, I had to attach an event to these buttons every time the contents of the ajax form were replaced using the ajax success for the event. Full code that rewrites the form and attaches the event to the cancel buttons:

 $(document).ajaxSuccess(function (event, xhr, settings) { var $jQval = $.validator, adapters, data_validation = "unobtrusiveValidation"; $jQval.unobtrusive.parse(document); $(".jsCancel").click(function (e) { $(e.currentTarget).closest("form").validate().settings.ignore = "*" }); }); 
+14
source

Take the button to submit the form using JavaScript.

Here is a good jQuery example:

 $("#MyButton").click(function(e) { //submit your form manually here e.preventDefault(); }); 
+3
source

This is really a comment on tugberk's answer, but the comments do not show code examples very well.

Some browser versions do not like the "preventDefault ()" function. After a few bites, I added a simple utility function:

 //Function to prevent Default Events function preventDefaultEvents(e) { if (e.preventDefault) { e.preventDefault(); } else { e.returnValue = false; } } 

You name it instead of "preventDefault" as follows:

 $("#CancelButton").on("click", function(event) { preventDefaultEvents(event); return false; }); 
+1
source

You can use the "cancel" css class. Example: <input type="submit" value="Cancel" name="Cancel" class="cancel" />

JQuery.Validate processes the rest in the following code:

 // allow suppresing validation by adding a cancel class to the submit button this.find("input, button").filter(".cancel").click(function() { validator.cancelSubmit = true; }); 
+1
source

Source: https://habr.com/ru/post/901996/


All Articles