我是javascript的入门者:

我需要一个函数来检测用户是否输入了日期..如果日期小于今天,则提醒他...

HTML:

 <input type="date" name="StartDate" id="userdate"  required />

我尝试过的JavaScript(当然不起作用):
function TDate() {
var UserDate = document.getElementById("userdate").value;
var ToDate = Date.now();
if (UserDate != ToDate) {
    alert("The Date must be Bigger or Equal to today date")
    return false;
}
return true;}

最佳答案

我制造了一个小提琴

https://jsbin.com/jidogo/edit?html,js,output

<input type="date" name="StartDate" id="userdate" onchange="TDate()" required />

JS
function TDate() {
    var UserDate = document.getElementById("userdate").value;
    var ToDate = new Date();

    if (new Date(UserDate).getTime() <= ToDate.getTime()) {
          alert("The Date must be Bigger or Equal to today date");
          return false;
     }
    return true;
}

09-12 20:32