我已经像这样使用了jquery.ui.datepicker.validation.js插件:
$(document).ready(function() {
$.datepicker.setDefaults({
dateFormat: 'yy-mm-dd',
firstDay: 1,
showOn: 'button',
changeMonth: true,
changeYear: true,
});
$("#mydate").datepicker();
$('#myform').validate({
errorPlacement: $.datepicker.errorPlacement,
rules: {
mydate: {
required: true,
dpDate: function(){traceBack(); return true;},
dpCompareDate: 'notBefore 2011-01-01'
}
},
messages: {
mydate: 'Please enter a valid date (yyyy-mm-dd) after 2011-01-01'
}});
function traceBack() {
console.log("Firing?");
}
});
编辑
如果您真的不希望在 datepicker 小部件中导航时触发任何验证事件,您可以在 datepicker 小部件可见时关闭 onfocusout 设置:
$(document).ready(function() {
$.datepicker.setDefaults({
dateFormat: 'yy-mm-dd',
firstDay: 1,
showOn: 'button',
changeMonth: true,
changeYear: true,
beforeShow: function () {$("#myform").validate().settings.onfocusout = false; },
onClose: function () {$("#myform").validate().settings.onfocusout = true;}
});
$("#mydate").datepicker();
$('#myform').validate({
errorPlacement: $.datepicker.errorPlacement,
rules: {
mydate: {
required: true,
dpDate: function(){traceBack(); return true;},
dpCompareDate: 'notBefore 2011-01-01'
}
},
messages: {
mydate: 'Please enter a valid date (yyyy-mm-dd) after 2011-01-01'
}});
function traceBack() {
console.log("Firing?");
}
});
看到这个小提琴:http: //jsfiddle.net/mccannf/Bn7gy/24/