11

I am trying to clear all error messages as well error highlighting when user click on the clear form button, below are actual requirements

  1. Validate form using Jquery validation
  2. When user click on a field error message should be cleared (per field)
  3. On clicking reset button, every error should be cleared

here is my code

$( document ).ready(function(){
   var validator = $("#register_form").validate({
   validator.resetForm();
   focusCleanup: true,
 rules: {
  // custom rules
},
messages: {
// custom messages
    }
  });

For me, first 2 things are working, but when I am trying to clear complete form, I am not able to do this.this is how I am trying to clear it

function clearInputForm(){
  alert("");
  var validator1 = $( "#register_form" ).validate();
  validator1.resetForm();
   $("#register_form")[0].reset();
}

But nothing is happening , though with $("#register_form")[0].reset();, form fields are getting clear, but error messages are not getting cleared.

3
  • see jsfiddle.net/arunpjohny/eV2Nd/2 Commented Feb 25, 2014 at 7:28
  • @ArunPJohny: Thanks, but in my case click event not getting triggered, issue seems to be with validator.resetForm(); inside $("#register_form").validate which is required to clear per fields error messages Commented Feb 25, 2014 at 7:34
  • So whatever happened with this? The answers below are not good enough for some reason? Commented Apr 2, 2014 at 1:24

4 Answers 4

12

Quote OP:

1) Validate form using Jquery validation

You cannot put the validator.resetForm(); method inside of the .validate() method.

.validate() is a plugin method where the only thing that can go inside is a comma separated map of the allowed options, {option:value, option:value, etc.}, as per the .validate() method documentation.

resetForm() method documentation

$("#register_form").validate({
    rules: {
        firstname: {
            required: true
        },
        lastname: {
            required: true
        },
        cell: {
            required: true
        }
    },
    messages: {
        // custom messages
    }
});

.validate() method DEMO: http://jsfiddle.net/P46gL/


Quote OP:

2) When user click on a field error message should be cleared (per field)

This is thanks to the focusCleanup option. As you've already done, set it to true and when you click on the field with an error, the error clears.

$("#register_form").validate({
    focusCleanup: true,
    rules: {
       ....

focusCleanup DEMO: http://jsfiddle.net/P46gL/1/


Quote OP:

3) On clicking reset button, every error should be cleared

You would call the resetForm() method from within a click handler of the reset button. This will immediately remove all error messages from the entire form and reset the validation plugin to its initial state.

$('#clearform').on('click', function () {
    $("#register_form").validate().resetForm();  // clear out the validation errors
});

Make sure the reset button is type="button" or type="reset" or it will trigger validation.

<input type="reset" id="clearform" value="reset form" />

Clear Errors DEMO: http://jsfiddle.net/P46gL/3/


Clearing out the field data

You can clear out the values of the fields by calling a JavaScript .reset() like this.

$('#clearform').on('click', function () {
    var form = $("#register_form");
    form.validate().resetForm();      // clear out the validation errors
    form[0].reset();                  // clear out the form data
});

Full Working DEMO: http://jsfiddle.net/P46gL/4/

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

Comments

4

$("#register_form")[0].reset();, form fields are getting clear, but error messages are not getting cleared.

to do this you can put one more line below it:

function clearInputForm(){
   alert("");
   var validator1 = $( "#register_form" ).validate();
   validator1.resetForm();
   $("#register_form")[0].reset();
   $('.error').hide();
}

Although you should do this way:

$( document ).ready(function(){
   var validator = $("#register_form").validate({
       focusCleanup: true,
       rules: {
         // custom rules
       },
       messages: {
         // custom messages
       }
    });
    $('[type="reset"]').on('click', function(){
       validator.resetForm();
    });
});

You should put your validator.resetForm(); in the click event of reset button.

2 Comments

with this, error message are not getting cleared at all
post some markup or better to create a jsfiddle for it.
1

If nothing works then try this approach (specially for clearing data purpose):

1- Form html:

<input type='reset' class="button_grey resetForm" value='Reset'>

2- Jquery validate

// you can add error fields
var validator = $("#clearform").validate({
      focusCleanup: true
});

3- Reset the form

$('[type="reset"]').on('click', function(){ 
      $("#clearform").closest('form').find("input[type=text], textarea").val(""); 
      $('input:checkbox').removeAttr('checked'); 
      validator.resetForm(); 
      return false;
});

1 Comment

You shouldn't do it this way. This manual hack will mess up the layout of the fields, and after that some errors may get displayed incorrectly.
0

All error messages will be cleared

$('.btn-reset').on('click', function () {
    $( "label.error" ).remove(); 
});

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.