jqueryerrorplacement

jquery errorPlacement


I have the following:

$("#pmtForm").validate({
            rules: {
                    acct_name: "required",                        
                    acct_type: "required",  
                    acct_routing:  { 
                                     required: true,    
                                     digits: true,
                                     exactLength:9
                                     }, 
                    acct_num:      { 
                                     required: true,    
                                     digits: true
                                     }, 
                    c_acct_routing:{ 
                                     equalTo: '#acct_routing'
                                     },     
                    c_acct_num:    { 
                                     equalTo: '#acct_routing'
                                     }      
            },
            messages: {
                    acct_name: "<li>Please enter an account name.</li>",
                    acct_type: "<li>Please choose an account type.</li>",
                    acct_routing: "<li>Please enter a routing number.</li>",
                    acct_num: "<li>Please enter an account number.</li>",
                    c_acct_routing: "<li>Please confirm the routing number.</li>",
                    c_acct_num: "<li>Please confirm the account number.</li>"
            },

        //  errorContainer: '#div.error',

            errorPlacement: function(error, element) {
                $('#errorList').html("");
                $('#errorList').append(error);
                $('div.error').attr("style","display:block;");  
            } 
        });

I am trying to insert the error messages to a div above the form. My problem is if I remove this line : $('#errorList').html(""); then it displays the error messages correctly the first time. If i hit the submit one more time, it will append another set of messages to the div. If I keep $('#errorList').html(""); then I will get only one error message.

  • Please enter an account number.
  • How do I refresh the errorList so it doesn't repeat itself and displays the error messages correctly?

    thanks in advance.


    Solution

  • this works:

      $("#addPmtAcctForm").validate({
                rules: {
                        acct_name: "required",                        
                        acct_type: "required",  
                        acct_routing:  { 
                                         required: true,    
                                         digits: true,
                                         exactLength:9
                                         }, 
                        acct_num:      { 
                                         required: true,    
                                         digits: true
                                         }, 
                        c_acct_routing:{ 
                                         equalTo: '#acct_routing'
                                         },     
                        c_acct_num:    { 
                                         equalTo: '#acct_num'
                                         }      
                },
                messages: {
                        acct_name: "<li>Please enter an account name.</li>",
                        acct_type: "<li>Please choose an account type.</li>",
                        acct_routing: "<li>Please enter a routing number.</li>",
                        acct_num: "<li>Please enter an account number.</li>",
                        c_acct_routing: "<li>Please confirm the routing number.</li>",
                        c_acct_num: "<li>Please confirm the account number.</li>"
                },
    
                errorLabelContainer: $("ul", $('div.error')), wrapper: 'li',
    
                errorContainer: $('div.error'),
    
                errorPlacement: function(error, element) {
                    $('#errorList').append(error);
                } 
            });