I am currently trying to create a form on a website, where a person fills in a date before submitting the form. If the user inputs a date before today, it will not submit the form and give an alert stating "Date cannot be before today"
.
However, I have run into an issue where my JavaScript algorithm is wrong and I can't find out what is causing the issue. The issue is: the date does not matter, and the user can input any date below 2020-01-01
from maxlength
from the form (JavaScript does not work)
function checkForm() {
var answ = validateDate(document.getElementById("doa").value);
if (answ == false) {
return false;
}
return true;
}
function validateDate(doa) {
var d = new Date(doa);
var n = new Date();
if (d <= n) {
alert("Date cannot be before today");
return false;
}
return true;
}
My Form:
<form action="advertResult.html" onsubmit="return checkForm()">
<label class="formText">First Name:</label>
<input type="text" id="firstName" name="firstname" placeholder="" required>
<label class="formText">Last Name:</label>
<input type="text" id="lastName" name="lastname" placeholder="" required>
<label for="commission" class="formText">Type of Commission</label>
<select id="commission" name="commission" required>
<option value="drawing">Art</option>
<option value="video">Video</option>
</select>
<label for="doa" class="formText">Select a date for discussion</label>
<input type="date" name="date" id="doa" max="2020-01-01" required>
<input type="submit" value="Submit">
</form>