I am stuck about 2 problems in JavaScript but I don't find the solution.
My first problem: we are on 25-05-2020
, if the user enters the date of yersteday or a previous date. I would like to display an error message.
I have to use the now()
method ? I tried this ??
const date = new Date(date_start)
if(date.getFullYear() < Date.now()) {
document.getElementById('date_startError').innerHTML = " ** the date is obsolete ! ";
return false;
}
My second problem: How to handle also the futur date? For example, I want to limit the date futur to one maximum year?
Example: we are on 2020-05-25
if the user enters on the input on 2020-05-27
I would like to display an error message.
Edit
function validation()
{
var date_start = document.getElementById('date_start').value;
const date = new Date(date_start);
const oneYearFromNow = now.setFullYear(now.getFullYear() + 1);
if(date < oneYearFromNow) {
document.getElementById('date_startError').innerHTML = " ** the date is obsolete ! ";
return false;
}
}
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Titre de la page</title>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<form action="#" onsubmit="return validation()" >
<br>
<label>Date start : </label>
<br>
<input type="date" name="date_start" id="date_start">
<br>
<span id="date_startError"></span>
<br>
<input type="submit" value="ok">
</form>
</body>
</html>