There are many ways to disable unobtrusive validation in Javascript but most of them seems a bit hackish...
- Recently found that you can do it with submit button. Check this link for info
http://www.nitrix-reloaded.com/2013/09/16/disable-client-side-validation-on-a-button-click-asp-net-mvc/
//this <script type="text/javascript"> document.getElementById("backButton").disableValidation = true; </script> //or this <input type="submit" name="backButton" value="Back" title="Go back to Prev Step" disableValidation="true" /> //or this <input type="submit" name="backButton" value="Back" title="Go back to Prev Step" class="mybtn-style cancel" />
- Another way that is more flexible but more complicated : you can disable unobtrusive validation by setting the
data-val
attribute to false
. But there is a trick...
Unobtrusive validation data is cached when the document is ready. This means that if you have data-val='true'
at the beginning and that you change it later on, it will still be true
.
So, if you want to change it after the document is ready, you also need to reset the validator which will erase the cached data. Here is how to do it :
disableValidation: function () { //Set the attribute to false $("[data-val='true']").attr('data-val', 'false'); //Reset validation message $('.field-validation-error') .removeClass('field-validation-error') .addClass('field-validation-valid'); //Reset input, select and textarea style $('.input-validation-error') .removeClass('input-validation-error') .addClass('valid'); //Reset validation summary $(".validation-summary-errors") .removeClass("validation-summary-errors") .addClass("validation-summary-valid"); //To reenable lazy validation (no validation until you submit the form) $('form').removeData('unobtrusiveValidation'); $('form').removeData('validator'); $.validator.unobtrusive.parse($('form')); },
You don't need to reset all the messages, input styles and validation summary to be able to submit the form but it's useful if you want to change the validation after the page is loaded. Otherwise, even if you change the validation, the previous error messages will still be visible...