-3

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">
RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
  • 2
    Please don't add the "Java" tag to Javascript questions. These two are as related to each other as Austria is to Australia. – RealSkeptic Dec 13 '17 at 16:42
  • 1
    Why "without using min and max" ? – mplungjan Dec 13 '17 at 16:43
  • You appear to be asking the user to enter today's date. Why? You are writing software. The user doesn't need to *decide* on what today's date is. Just ask the clock (on either the server or client depending on which timezone you care about). – Quentin Dec 13 '17 at 16:44

2 Answers2

-1

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" />
mplungjan
  • 169,008
  • 28
  • 173
  • 236
-2
// 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?

Jebussz
  • 1
  • 1