2012-08-28 154 views
0

現在我有一個自定義的datepicker,我試圖進行驗證。具體來說,我試圖檢查startDate字段是否在endDate字段之前。Jquery驗證自定義DatePicker

當我點擊我的日期選擇器並填充我的輸入字段時,我需要點擊我的字段,然後離開字段,然後纔會出現消息。我認爲這是因爲jquery validate正在監聽一個keyup事件,而不是輸入字段中的更改事件。

這是我的代碼(寫在咖啡,但我會接受JavaScript答案) -

validateSearchForm:=> 
$(@el).find("#searchForm").validate({ 
    rules:{ 
    startDateInputBox:{ 
     dateISO:true, 
     lessThan: true 
    }, 
    endDateInputBox:{ 
     dateISO:true 
     lessThan: true 
    } 
    searchPurposeBox:"required" 
} 
}); 

不到是 -

jQuery.validator.addMethod "lessThan", ((value, element) -> 
    validDateFormat = /(\d{4})-(\d{2})-(\d{2})/ 
    startDateInput = $("#startTime").val() 
    endDateInput = $("#endTime").val() 
    startDateMatch = startDateInput.match(validDateFormat) 
    endDateMatch = endDateInput.match(validDateFormat) 
    startDateYear = startDateMatch[1] 
    startDateMonth = startDateMatch[2] 
    startDateDay = startDateMatch[3] 
    endDateYear = endDateMatch[1] 
    endDateMonth = endDateMatch[2] 
    endDateDay = endDateMatch[3] 
    startDate = new Date(startDateYear,startDateMonth,startDateDay) 
    endDate = new Date(endDateYear,endDateMonth,endDateDay) 
    if endDate < startDate 
    return false 
    return true 
), "Start time has to be before end time" 

我如何使它所以,當我點擊在我的日期選擇器上填充我的字段時出現錯誤的值,那麼錯誤消息會立即顯示出來?我試過onkeyup,但是這並不能解決問題。

感謝

+0

檢查out [this plugin](https://github.com/elclanrs/jq-idealforms)如果你想自定義日期選擇器驗證。 – elclanrs

回答

0

什麼我以前做的是鉤到日期選擇器的OnClose事件(如果有的話,我知道了jQuery UI日期選擇器具有的OnClose),然後調用類似

$("#searchForm input[name='startDateInputBox']").valid()