2012-05-28 72 views
3

我有以下腳本來驗證我的表單。它工作正常,但我試圖顯示<span class="errormsg"></span>內的所有驗證錯誤消息,以逗號分隔,而不是在每個輸入旁邊單獨顯示它們。如何在一個範圍內顯示jquery驗證錯誤消息

我對jQuery的非常小的知識;我不知道如何做到這一點。你能告訴我嗎?

謝謝:)

<script> 
    $(document).ready(function() { 
    // validate signup form on keyup and submit 
    var validator = $("#signupform").validate({ 
     rules: { 
    customerid: "required", 
    date: "required", 
    invoiceid: { 
    required: true, 
    minlength: 2, 
    remote: "<? echo base_url();?>mycontroller/function" } 
    }, 
     messages: { 
    customerid: "Enter your customerid", 
    date: "Enter your date", 
    invoiceid: { 
     required: "Enter a invoiceid", 
     minlength: jQuery.format("Enter at least {0} characters"), 
     remote: jQuery.format("{0} is already in use") 
    }, 
     } 
    }); 
    }); 
</script> 

我的形式

<label>Invoice #</label> 
<input type="text" name="invoiceid" id="invoiceid" value="" /> 

<label style="margin-left:20px;">Date</label> 
<input type="text" id="datepicker1" name="date" value="" /> 

<label style="margin-left:20px;">Customer</label> 
<input type="text" id="customerid" name="customerid" value="" /> 

回答

13

我會用showErrors選項來實現這一點:

showErrors: function(errorMap, errorList) { 
    $(".errormsg").html($.map(errorList, function (el) { 
     return el.message; 
    }).join(", ")); 
}, 

例子:http://jsfiddle.net/bd3zM/

注:

  • 用途$.map建立基於所述errorList參數傳遞給該函數的錯誤信息的數組。
  • 然後使用.join(", ")將上面的數組連接成一個字符串。
  • 地方內部元件(一個或多個)的含量使用htmlerrormsg
+1

你是天才:)。在發佈這個問題之前,我在google上搜索了很多,但沒有找到任何內容。感謝很多人爲這個筆記。 :) –

+0

@black_belt:沒問題!很高興我能幫上忙。 –

相關問題