How to restrict jQuery UI Datepicker to disallow dates in the future?
I am using a jQuery UI Datepicker, such that only Sunday can be selected.
What I would like to happen is have it where no dates f开发者_开发百科rom the current date into the future can be selected. Here is the code I am using currently:
var daysToDisable = [1,2,3,4,5,6];
$('#startdate').datepicker({
beforeShowDay: disableSpecificWeekDays
});
function disableSpecificWeekDays(date) {
var day = date.getDay();
for (i = 0; i < daysToDisable.length; i++) {
if ($.inArray(day, daysToDisable) != -1) {
return [false];
}
}
return [true];
}
I would just use the single entry to restrict the date to nothing beyond today:
$('#startdate').datepicker({
maxDate: '+0d'
});
You can use the maxDate property to prevent the selection of future dates.
Note: this assumes your dateFormat is dd/mm/yy
Edit: http://jsfiddle.net/jXGZz/
var fullDate = new Date();
var twoDigitMonth = ((fullDate.getMonth().length+1) === 1)? (fullDate.getMonth()+1) : '0' + (fullDate.getMonth()+1);
$('#startdate').datepicker({
maxDate: fullDate.getDate() + "/" + twoDigitMonth + "/" + fullDate.getFullYear(),
beforeShowDay: disableSpecificWeekDays
});
function disableSpecificWeekDays(date) {
if(date.getDay()==0){
return [true];
}else{
return [false];
}
}
Modified twoDigitMonth
var twoDigitMonth = fullDate.getMonth()+1<10 ? "0"+fullDate.getMonth()+1 : fullDate.getMonth()+1 ;
please do call the datepicker like this
jQuery(document).ready(function() {
$('#startdate').datepicker({
beforeShowDay: disableSpecificWeekDays
});
});
精彩评论