How to validate current time using input tag in javascript without using min and max. If the date today is Dec 12, 2017 and i enter Dec 11, 2017 the process will not valid.
<input type="date" id="date" name="date">
How to validate current time using input tag in javascript without using min and max. If the date today is Dec 12, 2017 and i enter Dec 11, 2017 the process will not valid.
<input type="date" id="date" name="date">
Try this, assuming you want to test against PAST dates
window.onload = function() {
var dField = document.getElementById("date");
dField.oninput = function() {
this.classList.remove("error");
var d = new Date(this.value),
now = new Date();
now.setHours(0,0,0,0);
d.setHours(0,0,0,0);
if (now.getTime() > d.getTime()) {
this.classList.add("error");
}
}
}
.error {
border: 1px solid red
}
<input type="date" id="date" name="date" />
// date from input
var inputDate
// Get today's date
var todaysDate = new Date();
// call setHours to take the time out of the comparison
if(inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)) {
// Date equals today's date
}
This answer was taken from James Hill in this post: How to check if input date is equal to today's date?