【发布时间】:2026-02-05 01:15:01
【问题描述】:
我对 jquery 验证插件还很陌生。尝试比较两个字段中的日期值并在一个日期早于另一个日期时触发错误。这是标记:
<label>Last prescription fill date:</label>
<input type="text" ID="InputLastPrescriptionFillDate"
style="width: 200px"
Class="calendarSelectInput dateComparison required" />
<br />
<label>Prescription start date:</label>
<input type="text" ID="InputPrescriptionStartDate" name="InputPrescriptionStartDate"
style="width: 200px"
Class="calendarSelectInput dateComparison required" />
这里是 jQuery。
$(document).ready(function() {
$("form").validate({
rules: {
InputPrescriptionStartDate: {
required: compareRxDates()
}
},
messages: {
InputPrescriptionStartDate: {
required: "Last prescription fill date should not be after the prescription start date."
}
}
});
});
还有回调 javascript。
function compareRxDates() {
return new Date($("#InputPrescriptionStartDate").val()) < new Date($("#InputLastPrescriptionFillDate").val());
}
...它会在 document.ready 上调用,但不会在字段中的值更改时调用。我尝试将 form.validate 包装在这些字段的更改事件中,但仍然没有调用此函数。
我做错了什么?对于我正在尝试做的事情,这甚至是正确的方法吗?
【问题讨论】:
标签: jquery validation jquery-plugins