2012-08-02 96 views

回答

0

你可以在jQuery驗證插件中創建一個新的規則。檢查此鏈接jquery-validate - addMethod - how to apply custom rule referencing two text boxes?

在此規則中,您可以檢查表單字段並相應地返回規則的值。

或者您也可以更改字段的規則。假設你有一個字段「的firstName」,這樣你就可以像這樣

firstName: 
{ 
    required: function(element) 
    { 
     // here you can add your custom code 
    } 
} 
+0

非常感謝響應。我會嘗試。 – user519846 2012-08-02 09:54:36

0

添加自定義代碼,您可以使用addMethod規則這個定製驗證。 例如,如果您有一個窗體id =「mainForm」,並在下拉列表中顯示狀態列表。假設你的下拉是有ID =「drpState」的話,這樣寫

HTML 
------- 
<select> 
    <option value="-1">- Select -</option> 
    <option value="1">Aus</option> 
    <option value="2">Mogs</option> 
    <option value="3">Swut</option> 
</select> 

Jquery 
------- 
jQuery(document).ready(function() { 
$.validator.addMethod("selectState", function (value, element) { 
     return this.optional(element) || (value.indexOf('-1')); 
    }, "Please select state"); 

    $("#mainForm").validate({ 
     rules: {drpState:{selectState:true}, 
    }); 
}); 

請參見更多:http://ssthil.blogspot.in/2012/06/jquery-validate-multiple-fields-with.html 讓我知道你的反饋

相關問題