0

Each input has:

@Html.LabelFor
@Html.EditorFor
@Html.ValidationMessageFor

The validation message has a class of 'text-danger'. On submitting incorrect values, the errors appear properly. I added a Clear button with type="reset" but this only resets the input fields, not the validation messages.

I am aware of a similar question: How to clear Jquery validation error messages?

But, I need a clear explanation of whether to change the existing jquery.validation scripts, or how to write a separate script. Also, given the above criteria, how to format the onclick event of the reset button.

Community
  • 1
  • 1
B. Krinsky
  • 81
  • 4
  • 14
  • 1
    Travis J's answer is not a modification to the validation scripts. You'd add that code to your page. He gives two versions (the second version is minified). You would call `$("#formId").clearValidation();` when the clear button is clicked. – devlin carnate Oct 29 '15 at 16:14
  • Does it matter if the id of the form is declared on @Html.BeginForm or
    – B. Krinsky Oct 29 '15 at 17:51
  • you'd want the actual form id (@Html.BeginForm), and not the div, although if there isn't any extra content on your page other than what's in the form, it wouldn't matter. – devlin carnate Oct 29 '15 at 18:25
  • Ok thanks, that is where I have it. Now I am getting an error: **Cannot read property 'resetForm' of undefined** in the console. Do I have to change '[name]' in the clearValidation function? Just to clarify, on 'click' of the clear button I am calling the clearValidation function using the id of the form. – B. Krinsky Oct 29 '15 at 18:39

1 Answers1

1

I was getting a console error with Travis J's solution: 'cannot read property of 'resetForm' undefined...'

I used Nick Craver's answer to hide the error classes on Clear, and show them again on Submit.

<script type="text/javascript">
    $(document).ready(function () {
        $('#clear').click(function () {
            $(".has-error").hide();
            $(".text-danger").hide();
        });
        $('#submit').click(function () {
            $(".has-error").show();
            $(".text-danger").show();
        });
    });           

All validation still works.

B. Krinsky
  • 81
  • 4
  • 14