2012-04-21 49 views
0

我有一個訂單,表單的一部分是地址。它包含公司,街道地址,郵政編碼,城市,國家與一個簡單的jQuery表單驗證與每個領域的要求,並驗證郵編編號。現在我想要的是對它們進行分組,並且只有一個字段顯示成功時顯示「有效地址」,或者錯誤顯示「地址錯誤」。jquery表單驗證使用組

這是我的js代碼的一部分:

$("#pageform").validate(
{ 
    messages: { 
     company_from:  addressError, //addressError is a JS var for the error message 
     street_from:  addressError, 
     zip_from:   addressError, 
     town_from:  addressError, 
     country_from:  addressError 
    }, 
    groups: { 
     from: "company_from street_from zip_from town_from country_from" 
    }, 
    errorPlacement: function(error, element) { 
     if (element.attr("name") == "company_from" 
     || element.attr("name") == "street_from" 
     || element.attr("name") == "zip_from" 
     || element.attr("name") == "town_from" 
     || element.attr("name") == "country_from" 
     ) 
     { 
     $("#error_from").append(error); 
     } 
    }, 
    success: function(label) { 
     var attribute = $(label[0]).attr("for"); 
     $(".err-ok-" + attribute + " .ok").css("display", "block").css("visibility", "visible"); 
    } 
} 
); 

這是相應的HTML代碼的一部分:

<input type="text" name="company_from" id="company_from" class="required default input-s8" maxlength="255" /> 
<input type="text" name="street_from" id="street_from" class="required default input-s8" maxlength="255" /> 
<input type="text" name="zip_from" id="zip_from" class="required digits default input-s8" maxlength="5" onblur="checkCalculation()" /> 
<input type="text" name="town_from" id="town_from" class="required default input-s8" maxlength="255" /> 
<!-- and a select list for the country --> 

你不需要承擔我如何表現細看錯誤等等,我的問題是,那我不知道什麼時候顯示錯誤標籤,什麼時候成功標籤。當我爲郵編輸入一個字母時,我的errorPlacement函數和成功函數被調用(並且首先是errorPlacement),所以我猜想如果至少有一個字段正確,它總是調用成功。

請詢問是否有任何問題,我敢肯定有... :-)

回答

0

添加規則

rules: { 
    company_from: "required", 
    company_from: "required", 
    zip_from: "required", 
    town_from:"required", 
    country_from:"required" 
    }, 
+0

這不會改變任何東西。所需的是已經在我的HTML代碼爲CSS類。問題是,這些字段是分開評估的。 – Philipp 2012-04-25 15:56:50