我很新的jQuery验证插件。尝试比较两个字段中的日期值,并在一个日期早于另一个日期时触发错误。这里是标记:为什么此回调不起作用的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,但是这个函数仍然没有被调用。
我在做什么错?这是我正在尝试做的正确方法吗?
感谢回答。我试了一下,却无法让它工作。 – Marvin 2011-03-31 02:04:19
我更新了我的答案,但我看到我有点迟了:) – Daniel 2011-03-31 02:19:21
感谢您的帮助!我无法获得第一个解决方案,但第二个解决方案对我很好。 – Marvin 2011-03-31 02:27:01