javascriptjqueryjquery-uijquery-ui-datepicker

How to disable specific dates in jQuery UI's datepicker


Is there any (easy) way to set the jQuery UI Datepicker to disallow selection of specific, predetermined days?

I was able to get Adam Bellaire's answer to Can the jQuery UI Datepicker be made to disable Saturdays and Sundays (and holidays)? working, however, it produces a null error which prevents it from displaying in IE.

'natDays[...].0' is null or not an object

Most of my code was taken straight from the aforementioned answer:

natDays = [
        [7, 23], [7, 24], [8, 13], [8, 14], 
    ];
    
    function nationalDays(date) {
        for (i = 0; i < natDays.length; i++) {
            if (date.getMonth() == natDays[i][0] - 1
            && date.getDate() == natDays[i][1]) {
                return [false, natDays[i][2] + '_day'];
            }
        }
        return [true, ''];
    }
    
    function noWeekendsOrHolidays(date) {
        var noWeekend = $.datepicker.noWeekends(date);
        if (noWeekend[0]) {
            return nationalDays(date);
        } else {
            return noWeekend;
        }
    }
    
    
    $(function() { 
        $("#datepicker").datepicker({
            inline: true,
            minDate: new Date(2009, 6, 6), 
            maxDate: new Date(2009, 7, 14), 
            numberOfMonths: 2, 
            hideIfNoPrevNext: true,
            beforeShowDay: $.datepicker.noWeekends,
            altField: '#alternate',
        }); 
    });

Solution

  • Have you tried declaring natDays properly with the 'var' keyword in front?

    Also - you have an extra comma at the end of your natDays definition.

    natDays[i][2] won't work as your the arrays only have 2 items in them - try just ''

    Additionally, you'll want to set your beforeShowDay function name correctly - doesn't look like it's even calling your custom function