How do I clear MVC client side validation errors when a cancel button is clicked when a user has invalidated a form?


How do I clear MVC client side validation errors when a cancel button is clicked when a user has invalidated a form?



I have a partial view that is rendered within a main view. The partial view takes advantage of System.ComponentModel.DataAnnotations and Html.EnableClientValidation().

A link is clicked, and div containing the partial view is displayed within a JQuery.Dialog().

I then click the save button without entering any text in my validated input field. This causes the client side validation to fire as expected, and display the '*required' message beside the invalid field.

When the cancel button is clicked, I want to reset the client side MVC validation back to it's default state and remove any messages, ready for when the user opens the dialog again. Is there a recommended way of doing this?




Issue with jquery #find on partial postback

1:



Weird stack trace in exception “The incoming request does not match any route”
If you just want to clear the validation-messages so that they are not shown to the user you can do it with javascript like so:.
How to read system.web section from web.config
function resetValidation() {         //Removes validation from input-fields         $('.input-validation-error').addClass('input-validation-valid');         $('.input-validation-error').removeClass('input-validation-error');         //Removes validation message after input-fields         $('.field-validation-error').addClass('field-validation-valid');         $('.field-validation-error').removeClass('field-validation-error');         //Removes validation summary          $('.validation-summary-errors').addClass('validation-summary-valid');         $('.validation-summary-errors').removeClass('validation-summary-errors');      } 
If you need the reset to only work in your popup you can do it like this:.
Validate checkboxes in MVC form
function resetValidation() {         //Removes validation from input-fields         $('#POPUPID .input-validation-error').addClass('input-validation-valid');         $('#POPUPID .input-validation-error').removeClass('input-validation-error');         //Removes validation message after input-fields         $('#POPUPID .field-validation-error').addClass('field-validation-valid');         $('#POPUPID .field-validation-error').removeClass('field-validation-error');         //Removes validation summary          $('#POPUPID .validation-summary-errors').addClass('validation-summary-valid');         $('#POPUPID .validation-summary-errors').removeClass('validation-summary-errors');      } 
I hope this is the effect you seek..
ASP.NET MVC 2 - Checkboxes


displaying a listbox using Html.List, and I have a List<User> collection


showing errors from actions in table-based views

2:



Static DB Provider in ASP.NET MVC Causing Memory Leak
If you are using unobtrusive validation that comes with MVC you can simply do:.
$.fn.clearErrors = function () {     $(this).each(function() {         $(this).find(".field-validation-error").empty();         $(this).trigger('reset.unobtrusiveValidation');     }); }; 
------------------------------------------------------------------------. Third Party Edit: This mostly worked in my case, but I had to remove the $(this).find(".field-validation-error").empty(); line.

This appeared to affect the re-showing of the validation messages when resubmitting.. I used the following:.
$.fn.clearErrors = function () {         $(this).each(function() {             $(this).trigger('reset.unobtrusiveValidation');         });     }; 
and then called it like this:.
$('#MyFormId input').clearErrors(); 


3:


Thank you.

I had a similar question for a slightly different scenario.

I have a screen where when you click one of the submit buttons it downloads a file.

In MVC when you return a file for download, it doesn't switch screens, so any error messages which were already there in the validation summary remain there forever.

I certainly don't want the error messages to stay there after the form has been submitted again.

But I also don't want to clear the field-level validations which are caught on the client-side when the submit button is clicked.

Also, some of my views have more than one form on them.. I added the following code (thanks to you) at the bottom of the Site.Master page so it applies to all of my views..
    <!-- This script removes just the summary errors when a submit button is pressed      for any form whose id begins with 'form' --> <script type="text/javascript">     $('[id^=form]').submit(function resetValidation() {         //Removes validation summary         $('.validation-summary-errors').addClass('validation-summary-valid');         $('.validation-summary-errors').removeClass('validation-summary-errors');     }); </script> 
Thanks again..


4:


You can tap into the validation library methods to do this.

There are two objects of interest: FormContext and FieldContext.

You can access the FormContext via the form's __MVC_FormValidation property, and one FieldContext per validated property via the FormContext's fields property.. So, to clear the validation errors, you can do something like this to a form:.
var fieldContexts = form.__MVC_FormValidation.fields; for(i = 0; i < fieldContexts.length; i++) {     var fieldContext = fieldContexts[i];     // Clears validation message     fieldContext.clearErrors(); } // Clears validation summary form.__MVC_FormValidation.clearErrors(); 
Then, you can hook that piece of code to whichever event you need.. Sources for this (quite undocumented) insight:.


5:


function resetValidation() {.
    $('.field-validation-error').each(function () {         $(this).html("");     })  } 



79 out of 100 based on 89 user ratings 289 reviews

@