function validDate(value) { var arrD = value.split("."); arrD[1] -= 1; var d = new Date(arrD[2]+'.'+ arrD[1]+'.'+ arrD[0]+''); if ((d.getFullYear() == arrD[2]) && (d.getMonth() == arrD[1]) && (d.getDate() == arrD[0])) { return true; } else { alert("Введена некорректная дата!"); return false; } } 
  • in what format is the date entered? - Grundy

1 answer 1

The usual condition is:

 function validDate() { const inputDate = new Date(document.getElementById('inputDate').value).toISOString().slice(0, 10); // введенная дата (обрезанная до год-месяц-день) const currentDate = new Date().toISOString().slice(0, 10); // текущая дата (обрезанная до год-месяц-день) const res = (inputDate < currentDate); // сравниваем... (res) ? alert('ок') : alert('не ок'); // выводим ок или не ок return res; // возвращаем true или false } 
 <input id="inputDate" type="date" value="2019-02-01" required /><button onclick="validDate()">Проверить</button> 

No button:

 function validDate(val) { const inputDate = new Date(val).toISOString().slice(0, 10); // введенная дата (обрезанная до год-месяц-день) const currentDate = new Date().toISOString().slice(0, 10); // текущая дата (обрезанная до год-месяц-день) const res = (inputDate < currentDate); // сравниваем... (res) ? alert('ок') : alert('не ок'); // выводим ок или не ок return res; // возвращаем true или false } 
 <input type="date" value="2019-02-01" onblur="validDate(this.value)" required /> 

  • I have a realization without a button ((you need to check on the field - Peter Gagarsky February
  • @ PetrGagarsky Then it's even simpler) Added code above - Kvilios
  • Thanks for the help! I will sit down to read the method of verification! please tell me more: how to make the onchange check started after the full date entry (I mean the year (it works after the first key release)) - Peter Gagarsky February
  • @PetrGagarsky can put onblur instead of onblur , for example. Then the function will be launched each time when the focus is lost, which will give you the full date entry. - Kvilios
  • Oh, I read about onblur but apparently did not understand correctly !! - Peter Gagarsky February