1

Edit User and Clear User are separate buttons.

Then how will Clear the error message on click of Clear Button using following statement validator.resetForm(); ?

function clearUser(){ 
       // Need to clear previous errors here 
} 


function editUser(){     
    var validator = $("#editUserForm").validate({ 
            rules: {                             
                userName: "required" 
        },  
        errorElement: "span" ,                
        messages: { 
          userName: errorMessages.E2 
        } 
      }); 

     if(validator.form()){  
        // form submition code 

    } 
} 

2 Answers 2

7

I tried the solution but i found there is a litle mistake in the previous solution, you should do this

When you validate your form, you get the validator like this

var jqueryValidator = jQuery("#myForm").validate();

and when you need to reset the form you just invoke the code like this

jqueryValidator.resetForm();

Have a nice day =) I hope I can help you.

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

Comments

3

just make it global, eg:

var validator;
$(document).ready(function() {
       validator = $('#editUserForm');
});

function clearUser(){ 
       validator.resetForm();
} 

function editUser(){     
    validator.validate({ 
            rules: {                             
                userName: "required" 
        },  
        errorElement: "span" ,                
        messages: { 
          userName: errorMessages.E2 
        } 
      }); 

     if(validator.form()){  
        // form submition code 
    } 
} 

then you need to eliminate the var in editUser().

btw - you can also do:

function clearUser(){ 
       var validator = $('#editUserForm');
       validator.resetForm();
} 

3 Comments

Not a perfect solution ...Should i repeat the same thing var validator = $('#editUserForm'); in Clear button ?
you can ... but you need not ... you can also use my first approach, which takes a global variable ...
maybe this plugin offers a list of validators, so you can pick your desired one to clear it. otherwise: i fear nope

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.