I am trying to validate an HTML form field:
The start date must be before the end date
Both dates must be not in the past.
Here is my form inputs:
$(document).ready(function() {
var dateIsBefore = false;
var dateInPast = false;
$('#end_date').on('keyup', function() {
if ($('#start_date').val() < $('#end_date').val()) {
dateIsBefore = true;
} else {
$('#message').html('Start date must not be after end date').css('color', 'red');
dateIsBefore = false;
}
});
$('#submitRequest').click(function(e) {
if (!dateIsBefore) {
e.preventDefault();
}
})
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
<input class="form-control" id="start_date" name="start_date" placeholder="DD/MM/YYYY" required="required" />
<input class="form-control" id="end_date" name="end_date" placeholder="DD/MM/YYYY" required="required" />
<br/>
<button type="submit" id="submitRequest" class="btn btn-primary">Submit Request</button> <span id='message'></span>
I use type Date, so not sure if I need to replace /
before comparing. Also I am not sure how I would be able to check if the dates are in the past.