tags:

views:

441

answers:

1

Hi,

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 

    } 
} 
A: 

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();
} 
Andreas Niedermair
Not a perfect solution ...Should i repeat the same thing var validator = $('#editUserForm'); in Clear button ?
Yashwant Chavan
you can ... but you need not ... you can also use my first approach, which takes a global variable ...
Andreas Niedermair
Its working fine but is there is any other way ?
Yashwant Chavan
maybe this plugin offers a list of validators, so you can pick your desired one to clear it. otherwise: i fear nope
Andreas Niedermair