2010-05-27 101 views
0

我正在使用Jquery驗證插件,但是我需要添加一個「自定義規則」,我有2個日期字段,我需要確保結束日期不低於開始日期。我的問題是如何將兩個字段作爲元素傳遞。兩個字段的jQuery驗證插件

據我瞭解-u設置了自定義的功能是這樣的:

function customValidationMethod(value, element, params){ } 

,但不能看到我可以如何與兩個字段使用它,如果任何人有這將不勝感激任何想法。

回答

2

validation plugin docs provide a writeup for this,這裏的相關部分:

$.validator.addMethod("dateRange", function() { 
    return new Date($("#fromDate").val()) < new Date($("#toDate").val()); 
}, "Please specify a correct date range, the first must be before the second."); 

$("form").validate({ 
    //other rules, options, etc... 
    groups: { dateRange: "fromDate toDate" } //show one error message, not two 
}); 

注意,自定義方法使用的ID中,groups option使用name屬性。

+0

歡呼尼克 - 奇怪我沒有碰到那篇文章之前感謝隊友 – jonnyhitek 2010-06-01 13:39:59

相關問題