I'm trying to validate a date input box so that it only accepts the current or future dates. So far I've been struggling to find answers which definitively does this.
Here is the HTML for the input box, excluding the <form>
tags:
<p>
<label>Date:</label>
<br>
<input type="number" name="date" placeholder="DD/MM/YYYY" onchange="checkDate()">
</p>
<div id="datewarn"></div>
Here is JavaScript code that I'm using which validates whether the input is in the format DD/MM/YYYY and that the numbers entered in are valid calender numbers, but this still accepts past dates.
function checkDate() {
var valid = true;
var redate = /(0[1-9]|[12][0-9]|3[01])[/](0[1-9]|1[012])[/](19|20)dd/;
if (!redate.test(document.bookingsform.date.value)) {
document.bookingsform.date.style.border = "1px solid red";
document.getElementById("datewarn").innerHTML = "Enter a date in the format DD/MM/YYYY.";
document.bookingsform.date.title = "Please enter a date in the format DD/MM/YYYY.";
document.getElementById("datewarn").style.display = "block";
valid = false;
} else {
document.bookingsform.date.style.border = "1px inset #EBE9ED";
document.bookingsform.date.style.borderRadius = "2px";
document.getElementById("datewarn").style.display = "none";
}
}
The research that I have done suggests using the date.js library? Is this an inbuilt library or this something I have to get?
This can only be JavaScript, no jQuery.
EDIT: Sorry, forgot to add the RegEx variable.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…