I have a simple form that contains one submit button and 2 input text, that call "date1" and "date2" and both are using DatePicker Gijgo plugin (https://gijgo.com/datepicker) to select date.
I would like to disable submit button if i select date from "date2" that is less than today and enable submit button if i select date from "data2" that is greater/equal than today.
Before i post here, i researched the topics below, but i didn't find the correct way to solve this:
- JQuery Datepicker OnSelect and TextChanged problem
- Activate button when two datepickers fields have values
- jQuery disable or enable submit button based on dates
Below is the code that i'm working:
1. CDN Scripts
<!-- BOOTSTRAP CSS CDN -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" />
<!-- BOOTSTRAP SCRIPT CDN -->
<script src="https://code.jquery.com/jquery-3.4.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<!-- DATEPICKER SCRIPT CDN -->
<script src="https://unpkg.com/gijgo@1.9.13/js/gijgo.min.js" type="text/javascript"></script>
<link href="https://unpkg.com/gijgo@1.9.13/css/gijgo.min.css" rel="stylesheet" type="text/css" />
2. HTML form code
<div class="container" style="max-width:507px;">
<br>
<form method="post" id="date_form" enctype="multipart/form-data">
<div class="form-row">
<div class="col-md-6 mb-3">
<label align="left"><b>Date 1</b></label>
<input type="text" name="date1" id="date1" class="form-control"/>
</div>
<div class="col-md-6 mb-3">
<label align="left"><b>Date 2</b></label>
<input type="text" name="date2" id="date2" class="form-control"/>
</div>
</div>
</form>
<div class="form-footer">
<input type="submit" id="btnSubmit" name="btnSubmit" class="btn btn-success" value="SUBMIT" disabled/>
</div>
</div>
- DatePicker script that disable old date from "date2" before the date that was selected at "date1":
<!-- DATEPICKER SCRIPT -->
<script>
var today = new Date();
$('#date1').datepicker({
format: 'dd/mm/yyyy',
uiLibrary: 'bootstrap4',
iconsLibrary: 'fontawesome',
maxDate: function () {
return $('#date2').val();
}
});
$('#date2').datepicker({
format: 'dd/mm/yyyy',
uiLibrary: 'bootstrap4',
iconsLibrary: 'fontawesome',
minDate: function () {
return $('#date1').val();
}
});
</script>
4. And finaly, the code that i'm trying to apply to disable submit button when i selected a date from "date2" that is less (<) than today, but unfortunately it doesn't works:
<!-- SCRIPT TO DISABLE SUBMIT BUTTON IF ***date2*** IS LESS THAN TODAY -->
<script>
$('#date2').datepicker({
dateFormat: 'dd/mm/yyyy',
onSelect: function(dateText){
var date2 = $('#date2').val();
var curDate = new Date();
if(date2 < curDate){
$("#btnSubmit").attr("disabled", true);
}else{
$("#btnSubmit").attr("disabled", false);
}
}
});
</script>
In this case, how can i modify this script above to disable submit button when i select each time a date from "date2" that is less than today and enable submit button when i select a date from "date2" that is greater/equal than today?