Share via


How to remove a validation with jQuery?

Question

Saturday, February 4, 2012 9:50 AM

Hi

I am using these scripts for asynchronous validation:

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>

<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

I would like to remove a validation message with jQuery after it has been generated. How do I do that?

Here is the code which is generating the validation error message:

        <div class="editor-field">             @Html.ValidationMessageFor(model => model.MyField)         </div>

How to remove the validation error message once it has been generated with jQuery?

 

All replies (4)

Saturday, February 4, 2012 2:10 PM âś…Answered

Hi zuperboy90

Thank you for your response to my problem.

The solution to my problem turned out to be quite simple, I just needed an ID to hide the error message dynamically with jQuery:

 

<div id="validation-message">    

         @Html.ValidationMessageFor(model => model.MyField)

</div>

 

$("#validation-message").hide();


Saturday, February 4, 2012 9:52 AM

Remove it or replace it?

To remove it, just don't put @Html.ValidationMessageFor(model => model.MyField) on the page.


Saturday, February 4, 2012 9:56 AM

I want to remove it with jQuery hide() after it has been generated. But I need to know its ID?

Perhaps I shall give @Html.ValidationMessageFor(model => model.MyField) an ID myself?


Saturday, February 4, 2012 10:00 AM

By default, it is hidden. It becomes visible only when you try to submit the form and the validation rule is invalid.

If you made valid the input which was invalid (with javascript), you can call $("#Name").valid() and it will re-evaluate the value and hide the invalid error message.

Is this what are you trying to do?