gbvisconti
gbvisconti

Reputation: 412

Datepicker Jquery only allow set of dates

How could I set datepicker to only allow selecting (for any year) '05/01', '08/01', '11/01', '01/02'?

$(".dateField").datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat: "yy/mm/dd",
    yearRange: '1900:2100'
});

If that is even possible, of course! Thanks in advance!

Upvotes: 0

Views: 60

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

You can use beforeShowDay to disable all other days(but it won't look good because almost all of the calendar will look disabled and will be harder to find out the enabled date)

$(".dateField").datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat: "yy/mm/dd",
    yearRange: '1900:2100',
    beforeShowDay: function (date) {
        return [date.getDate() == 1 && [5, 8, 11, 1].indexOf(date.getMonth() + 1) >= 0]
    }
});
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/redmond/jquery-ui.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.js"></script>

<input class="dateField" />

It will be much better if you provide 2 select boxes(one with years and other with allowed dates) instead of a datepicker

Upvotes: 1

Related Questions