Fares B Bader Fares B Bader - 5 months ago 11
HTML Question

javascript function (input date bigger than today date)

I'm starter in javascript :

I need a function that detect if user input a date .. and alert him if the date is smaller than today date ...

HTML :

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


JavaScript I tried (didn't work of course ):

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;}

Answer

I have created a fiddle

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;
}
Comments