0

I am a newbie to the Jquery and I am developing an Hotel application, where the users are allowed to select the 'check-in' and 'check-out' dates, and I am using a datepicker for these fields. Logically, the 'check-out' date can either be equal to or more than the 'check-in'. I have written an validation that can check this condition but, I want to restrict the user on the UI itself, to select date for 'check-out' that are only equal to or more than 'check-in' date, by disabling all the dates that fall before 'check-in' in the date picker for 'check-out'.. I have browsed a lot, but no luck for this criteria.

Jquery UI datepicker. Disable array of Dates

I have referred the above, but the problem is, they have defined a small array of dates for which they have disabled the select. But, in my case, I want to disable all the dates that fall before 'check-in' date in the 'check-out' date picker.

How can I do that? Can someone help me please......

Thanks in Advance....

Community
  • 1
  • 1
user2771399
  • 135
  • 1
  • 4
  • 15

2 Answers2

2

set your seleted date as mindate like as

$( ".selector" ).datepicker({ minDate: 'selected-date' });

It will disable all dates before min date.

for example:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>datepicker demo</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
</head>
<body>
<div id="check-in"></div>
<div id="check-out"></div>
<script>
    $( "#check-out" ).datepicker({
    minDate: new Date()
});

$( "#check-in" ).datepicker({
    minDate: new Date(),
    onSelect: function(dateText, inst) {
        var selectedDate = $( this ).datepicker( "getDate" );
        $( "#check-out" ).datepicker( "option", "minDate", selectedDate );
    }
});
</script>
</body>
</html>
Harish Singh
  • 3,359
  • 5
  • 24
  • 39
2

Read date range with DEMO

js

$(function () {
    $("#from").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        onClose: function (selectedDate) {
            $("#to").datepicker("option", "minDate", selectedDate);
        }
    });
    $("#to").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        onClose: function (selectedDate) {
            $("#from").datepicker("option", "maxDate", selectedDate);
        }
    });
});

HTML

<label for="from">From</label>
<input type="text" id="from" name="from" />
<label for="to">to</label>
<input type="text" id="to" name="to" />

Also Read

http://api.jqueryui.com/datepicker/#option-minDate

http://api.jqueryui.com/datepicker/#option-maxDate

http://api.jqueryui.com/datepicker/#option-onClose

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107