In my asp.net mvc page I need to have a "clear" button that resets certain input fields and clears all the error messages. Clearing the inputs is fine but because the validation messages are generated by the asp.net mvc javascript it's not obvious to me how to clear them?
Update: This ended up working well for me.
$(".field-validation-error").empty();
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.
Adding an else statement to return the value of your error message span to a non-error state could correct this. this should display your error msg and return it to blank when a valid input is present.
You can view the html code generated in the browser and then just empty it through jquery e.g. empty()
To find the generated code:
Hope this helps
$('.field-validation-error').text("")
A simple and reusable jQuery function you can call on any jQuery object:
$.fn.clearErrors = function () {
$(this).each(function() {
$(this).find(".field-validation-error").empty();
$(this).trigger('reset.unobtrusiveValidation');
});
};
Usage:
question.clearErrors();
Original answer: https://stackoverflow.com/a/16165831/114029
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With