9

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;}
Fares B Bader
  • 103
  • 1
  • 1
  • 8
  • What format is userdate? What today do you want? The users today or your possibly different today? – Alex K. Nov 18 '15 at 15:18
  • the system or server today .... example : user input 17/11/2015 but today in server or in system date is : 18/11/2015 I need the function to alert the user that he inserted a restricted date and focus on date to change it – Fares B Bader Nov 18 '15 at 15:25
  • kindly mark the best answer :) – Zohaib Ijaz Nov 18 '15 at 15:28

4 Answers4

23

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;
}
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60
  • it is good ,,, but when I insert the date manually it start alerting from the first value in the year section .... I think it need a button and an (onclick) event ... right ? – Fares B Bader Nov 18 '15 at 15:31
  • Actually I did this earlier but then changed as I was expecting that you need validation on change, but you are right, add a button like `` and remove `onchange` attribute from input – Zohaib Ijaz Nov 18 '15 at 15:33
2

I assume that you can get the selected Date object by var selectedDate = document.getElementById("userdate").value. Then you can compare two date like compare 2 integer number:

function TDate() {
    var selectedDate = document.getElementById("userdate").value;
    if (selectedDate < Date.now()) {
        alert("The Date must be Bigger or Equal to today date")
        return false;
    }
    return true;
}
Long Nguyen
  • 11,075
  • 2
  • 18
  • 25
0

I think this would be cleaner than other answers thanks to Juank:

var now = new Date(),
    minDate = now.toISOString().substring(0,10);

$('#my-date-input').prop('min', minDate);
Community
  • 1
  • 1
Morteza Tourani
  • 3,506
  • 5
  • 41
  • 48
0

the accepted answer checks by time.
as the input type is "date", the milliseconds are 000
to compare for day

var variable1 = new Date();
var variable2 = variable1.toISOString().split('T')[0];
if (new Date($("#userdate").val()).getTime() < new Date(variable2).getTime()) {
    //input date less than todays date
}else{
    //input date bigger than todays date
}
sifr_dot_in
  • 3,153
  • 2
  • 33
  • 42