我會建議使用jQuery's datepicker做客戶端上的日期範圍的約束。您需要禁用輸入元素<input readonly/>
。我最近實現了一個適用於我的日期範圍約束。它限制結束日期在開始日期的90天之內
僅供參考,我停止使用startElement和endElement作爲測試,從未返回,因爲此代碼當前未被重用。
// ============================================================================
// FUNCTION: InitializeDates(startElement, endElement)
// PARAMETERS
// ----------
// startElement: The element which will be initialized as the start-date
// datepicker.
//
// endElement: The element which will be initialized as the start-date
// datepicker.
//
// DESCRIPTION
// -----------
// InitializeDates updates the start and end dates for non-employees. It
// creates a date-picker object on both fields and then constrains the end
// end date:
// * No date selections available prior to the start date
// * No date selections available after 90 days beyond the start date.
// ----------------------------------------------------------------------------
function InitializeDates(startElement, endElement)
{
$("#f-start-date").datepicker({
showOn: "button",
buttonImage: "images/calendar.png",
buttonImageOnly: true,
onSelect: function(dateText, inst) { StartDateSelected(dateText, inst) }
});
$("#f-end-date").datepicker({
showOn: "button",
buttonImage: "images/calendar.png",
buttonImageOnly: true,
numberOfMonths: 3
});
$("#f-start-date").val('');
$("#f-end-date").val('');
}
// ============================================================================
// FUNCTION: StartDateSelected(dateText, endElement)
// PARAMETERS
// ----------
// dateText: The dateText passed from jQuery.datepicker.onSelect
// inst: The instpassed from jQuery.datepicker.onSelect//
//
// DESCRIPTION
// -----------
// Updates the end-date maxDate and minDate fields to 91 dates from the selected
// start date.
// ---------------------------------------------------------------------------
function StartDateSelected(dateText, inst)
{
var second = 1000;
var minute =second * 60;
var hour = minute * 60;
var day = hour * 24;
// The datepicker updates maxDate and minDate based on today's date. I've
// got to math out dates so that the maxDate is actually 91 days from the
// selected date.
var todaysDate = new Date();
var selectedDate = new Date(dateText);
var duration = Math.floor((selectedDate - todaysDate)/day) + 91;
$("#f-end-date").datepicker("option", "minDate", selectedDate);
$("#f-end-date").datepicker("option", "maxDate", duration);
$("#f-end-date").val('');
}
我也有興趣學習這個特定的MVC 3驗證事情...檢查這一個:http://forums.asp.net/t/1698991.aspx/1希望它有助於作爲一個開始。 –
我誤解了你的問題嗎?您是否正在使用MVC 3驗證來驗證日期,或者編寫自定義JS? – Rastapopulous
哪一個。我只是希望它以同樣的方式工作 – Doomsknight