私は次のように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?");
}
});
編集
日付ピッカーウィジェット内を移動しているときに検証イベントを発生させたくない場合は、日付ピッカーウィジェットが表示されているときに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/