我在我的 asp.net mvc 页面上使用 jquery daterange 验证器http://docs.jquery.com/Plugins/Validation/multiplefields
。我的 html 看起来像这样:
<tr>
<td align="left" valign="top">
<label>
<input name="fromDate" type="text" class="flat requiredDateRange jq_watermark" id="fromDate"
style="width: 190px" title="Creation From Date" readonly="readonly" />
</label>
</td>
</tr>
<tr>
<td align="left" valign="top">
<label>
<input name="toDate" type="text" class="flat requiredDateRange jq_watermark" id="toDate"
style="width: 190px" title="Creation To Date" readonly="readonly" />
</label>
</td>
</tr>
<tr>
<td colspan="3" align="left" valign="top">
<label id="error" class="error" style="display: block;">
</label>
</td>
</tr>
在页面顶部,我正在使用这个:
$(document).ready(function(){
$("#fromDate").datepicker({ dateFormat: 'dd MM yy', changeYear: true, changeMonth: true });
$("#toDate").datepicker({ dateFormat: 'dd MM yy', changeYear: true, changeMonth: true });
});
和
jQuery(function () {
jQuery("#frmSearch").validate({
groups: {
datesnotnull: "fromDate toDate",
dateRange: "fromDate toDate"
},
errorPlacement: function (error) {
jQuery("#frmSearch").find("#error").append('error');
jQuery("#frmSearch").find("#error").show();
}
});
});
我的 daterangevalidator 文件如下所示:
// a custom method for validating the date range
$.validator.addMethod("datesnotnull", function () {
return (($("#fromDate").val().length != 0) && ($("#toDate").val().length != 0)) || (($("#fromDate").val().length == 0) && ($("#toDate").val().length == 0));
}, "Please specify the date range, from and to date.");
// a custom method for validating the date range
$.validator.addMethod("dateRange", function () {
return ((($("#fromDate").val().length==0) && ($("#toDate").val().length==0)) || (new Date($("#fromDate").val()) < new Date($("#toDate").val())));
}, "Please specify a correct date range, the from date must be before the to date.");
// a new class rule to group all three methods
$.validator.addClassRules({
requiredDateRange: { required: false, date: true, datesnotnull : true , dateRange: true }
});
// overwrite default messages
$.extend($.validator.messages, {
date: "Please specify valid dates"
});
但是当 todate 小于数据时,我只收到“Creation To Date”消息。我想显示消息“请指定正确的日期范围,起始日期必须早于截止日期”,这是红色的默认消息