23

How do I reset a form?

i.e. Clear values of all fields and remove ValidationSummary error messages validation-summary-errors with jquery.

I use the below code but it does not work:

    var validator = $("#myform").validate();
    validator.resetForm();

I'm using asp.net MVC3 and the jquery scripts are include in my page.

<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<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>
1
  • @Ken: Good point, sorry for not being more verbose. Commented Mar 28, 2011 at 13:09

7 Answers 7

54

I wrote a quick jQuery extension to handle this issue when I encountered it which:

  • Clears field level validation errors
  • Clears/hides validation summary
  • Resets jQuery Validate's internal error list

It can be called from an element $(selected) within the form or on the form its self.

Here is a calling example (the input is within a form):

<input onclick="$(this).resetValidation()" type="reset" value="Reset" />

Here is the jQuery plugin code:

(function ($) {

    //re-set all client validation given a jQuery selected form or child
    $.fn.resetValidation = function () {

        var $form = this.closest('form');

        //reset jQuery Validate's internals
        $form.validate().resetForm();

        //reset unobtrusive validation summary, if it exists
        $form.find("[data-valmsg-summary=true]")
            .removeClass("validation-summary-errors")
            .addClass("validation-summary-valid")
            .find("ul").empty();

        //reset unobtrusive field level, if it exists
        $form.find("[data-valmsg-replace]")
            .removeClass("field-validation-error")
            .addClass("field-validation-valid")
            .empty();

        return $form;
    };
})(jQuery);

Hopefully this helped! You can read more about it and see some other examples on my blog post here as well:

http://www.johnculviner.com/post/2011/11/16/ClearReset-MVC-3-Form-and-Unobtrusive-jQuery-Client-Validation.aspx

Sign up to request clarification or add additional context in comments.

5 Comments

Haha sweet! Glad its still useful :)
Tried four suggestions. This was the one that worked.
@JohnCulviner you just saved my precious time..thanks
It seems there also should be something like $form.find(".input-validation-error").removeClass("input-validation-error");
Spent hours trying different suggestions on SO and elsewhere but to no avail until I found this one! Great answer! Thanks.
33
$('.field-validation-error')
    .removeClass('field-validation-error')
    .addClass('field-validation-valid');

$('.input-validation-error')
    .removeClass('input-validation-error')
    .addClass('valid');

2 Comments

I was expecting resetForm() to do this.. I was wrong. Thank you
I found this to work for me $('.field-validation-error').remove();
9

you could use native solution (validate 1.9.0 + unobtrusive)

var $form = $(this).closest('form');

// reset errors with unobtrusive
$form.trigger('reset.unobtrusiveValidation');

// reset inputs
var validator = $form.validate(); // get saved validator
//validator.settings.ignore = ''; // if form hided (closed ui dialogs, etc)
validator.resetForm();

2 Comments

You don't need to do the resetForm at the end. The triggered event does it internally.
This works, but the validation summary remains invisible on subsequent form submits.
4

To follow up from Darin Dimitrov's message the following will also clear the validation summary...

$(".validation-summary-errors")
        .removeClass("validation-summary-errors")
        .addClass("validation-summary-valid");

Comments

1

To completement Darin and Kennifer answer, I put everything together and added a last part to reenable lazy validation

        //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'));

Comments

1

The following code uses find to search first for the container then the child list. If the list is empty the class validation-summary-valid is added to the container and validation-summary-errors removed.

var container = $('form').find('[data-valmsg-summary="true"]');
var list = container.find('ul');

if (list && list.length) {
    list.empty();
    container.addClass('validation-summary-valid').removeClass('validation-summary-errors');

Comments

0

Hope this helps you http://www.electrictoolbox.com/jquery-clear-form/

Happy coding

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.