Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset MVC form with jquery

Tags:

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> 
like image 298
Ghooti Farangi Avatar asked Mar 28 '11 10:03

Ghooti Farangi


People also ask

How to reset a form with JQuery?

JQuery doesn't have a reset() method, but native JavaScript does. So, we convert the jQuery element to a JavaScript object. JavaScript reset(): The reset() method resets the values of all elements in a form (same as clicking the Reset button).

How to clear form input after submit in JQuery?

ready(function() { submitForm(); }); As you can see from the above function we have resetForm() to call the form and reset the form data.

How do I reset form data?

reset() method restores a form element's default values. This method does the same thing as clicking the form's <input type="reset"> control. If a form control (such as a reset button) has a name or id of reset it will mask the form's reset method. It does not reset other attributes in the input, such as disabled .

How do you clear a form in HTML?

The <input> tag, helps you to take user input using the type attribute. To clear all the input in an HTML form, use the <input> tag with the type attribute as reset.


2 Answers

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

like image 161
John Culviner Avatar answered Sep 20 '22 13:09

John Culviner


$('.field-validation-error')     .removeClass('field-validation-error')     .addClass('field-validation-valid');  $('.input-validation-error')     .removeClass('input-validation-error')     .addClass('valid'); 
like image 32
Darin Dimitrov Avatar answered Sep 21 '22 13:09

Darin Dimitrov