I have two input date using daterangepicker, input one
and input two
. I want to know how to make maxDate
on input two
after user choose the date on input one
. I have success add endDate
setting on input two
, but I need maxDate
setting for prevent user choose over the date they choose on input one
Here’s the html code
<input type="text" name="input_one" id="input_one" />
<input type="text" name="input_two" id="input_two" />
Here’s js code
$('input[name="input_one"]').daterangepicker({
minDate:moment(),
timePicker: false,
singleDatePicker: true,
autoApply: true,
autoUpdateInput: true,
locale: {
format: 'DD-MM-YYYY'
}
}, function(start, end, label){
var selectedEndDate = end.format('DD-MM-YYYY');
$checkinInput = $('#input_one');
var checkInPicker = $checkinInput.data('daterangepicker');
checkInPicker.setEndDate(selectedEndDate);
})
$('input[name="input_two"]').daterangepicker({
minDate:moment(),
timePicker: false,
singleDatePicker: true,
autoApply: true,
autoUpdateInput: true,
locale: {
format: 'DD-MM-YYYY'
}
})
thanks
2
Answers
I've found a solutions, so before
input two
daterangepicker functions, addonchange
function based oninput one
clicked.Here's the code
To set the
maxDate
oninput_two
based on the selected date frominput_one
, you can modify your JavaScript code as follows:User selects a date in
input_one
, the selected end date is obtained and stored in theselectedEndDate
variable. Then, theminDate
option ofinput_two
is set to theselectedEndDate
value, which prevents the user from selecting a date earlier than that. Additionally, if the currently selected date ininput_two
is before the newminDate
, it clears the selected date ininput_two
and updates the input value to an empty string.Let’s understand through the code step by step to understand what each part does:
1.Initialization of
input_one
:This code initializes the
input_one
date range picker with the specified options. It also provides a callback function that will be executed when a date is selected ininput_one
.2.Callback function for
input_one
:This is the callback function that is called when a date is selected in
input_one
. It performs the following tasks:input_one
and stores it in theselectedEndDate
variable.input_two
using its ID (#input_two
) and assigns it to the$checkoutInput
variable.minDate
option ofinput_two
to theselectedEndDate
value using thesetOptions
method.input_two
and assigns it to thecheckoutPicker
variable.input_two
is before the newminDate
. If it is, it clears the selected date ininput_two
and updates the input value to an empty string.Initialization of
input_two
:$(‘input[name="input_two"]’).daterangepicker({
// Options for input_two
});
This code initializes the
input_two
date range picker with the specified options.Overall, the code sets the
minDate
option ofinput_two
based on the selected end date frominput_one
. It also ensures that if the currently selected date ininput_two
is before the newminDate
, the selected date is cleared. This helps prevent users from choosing a date ininput_two
that is earlier than the selected date ininput_one
.