I have some date comparison code that I am trying to adjust to my own project. I don't know much about javascript so I am trying to make sense of it. Basically the functions are set up but I also want them to display a message saying 'that date is invalid'. I started on the function not sure if it makes sense I copied it from some other javascipt The code is inline for now but I will separate everything later on. The CSS is from the original example. This was not showing though when I tested it but I could see in the console the javascript was working. Here is my code:
NOTE: I would like javascript not jquery solutions please, just for my assignment it is required that we use javascript only.
<style>
input.okay{
color: green;
}
input.invalid{
color: yellow;
}
</style>
<h1>Date comparrison</h1>
<form action="">
<div class="row">
<label for="arrival">Arrival</label>
<input type="date" id="arrival">
</div>
<div class="row">
<label for="checkout">Checkout</label>
<input type="date" id="checkout">
</div>
<div class="row">
<input type="submit" id="submit">
</div>
</form>
<script>
var submit_button = document.querySelector('#submit');
var arrival = document.querySelector('#arrival');
var checkout = document.querySelector('#checkout');
checkout.onblur = checkDates;
function checkDates() {
var valid = true;
if(isValidDate(arrival.value) && isValidDate(checkout.value)) {
valid = false;
var arrival_date = new Date(arrival.value).valueOf();
var checkout_date = new Date(checkout.value).valueOf();
if(arrival_date > checkout_date) {
valid = false;
}
} else {
valid = false;
displayError('arrival', 'That date is invalid');
} else {
hideError('arrival');
return valid;
}
}
function isValidDate(str) {
var datePattern = /^\d{2,4}\-\d{1,2}\-\d{1,2}$/;
return datePattern.test(str);
}
submit_button.onclick = function() {
console.log(checkDates());
return false;
}
</script>