I'm having trouble with ensuring that only today's date or a future date can be entered into my form. The function I'm using is supposed to accept the future dates, but for some reason, it's not working as expected. Can you help me figure out why?
//Here is the function that checks if the input date is either today or in the future
function isFutureDate(idate) {
var today = new Date().getTime(),
idate = idate.split("/");
idate = new Date(idate[2], idate[1] - 1, idate[0]).getTime();
return (idate - today) >= 0 ? true : false;
}
I also have another function that uses both RegEx and the above function:
function checkDate() {
var redate = /((0)[1-9]|(1)[0-9]|(2)[0-9]|(3)[0-1])[\/](0[1-9]|1[012])[\/](20)(1)[4-7]/;
var valid = true;
var idate = document.getElementById("date");
if (redate.test(idate.value)) {
if (isFutureDate(idate.value)) {
idate.style.border = "1px inset #EBE9ED";
idate.style.borderRadius = "2px";
document.getElementById("datewarn").style.display = "none";
} else {
idate.style.border = "1px solid red";
idate.style.borderRadius = "2px";
document.getElementById("datewarn").style.display = "none";
}
} else {
idate.style.border = "1px solid red";
document.getElementById("datewarn").innerHTML = "Enter a date in the format DD/MM/YYYY.";
idate.title = "Please enter a date in the format DD/MM/YYYY.";
document.getElementById("datewarn").style.display = "block";
valid = false;
}
}
Below is the HTML code snippet for the Date Input field:
<input type="text" id="date" placeholder="DD/MM/YYYY" onkeyup="checkDate()">