There is <input type=date>
field in my html
page. And I am trying to make a function in javascript
that will set today's date in that field if some date greater than today has been chosen. Actually, I would like to restrict maximum date to today's date. Here is code in jsfiddle. What am I doing wrong?
HTML:
<div class="entry">
<label for="startDate">Start date:</label>
<input id="startDate" name="startDate" type="date" oninput="validDate(startDate)">
</div>
Javascript:
function validDate(date) {
todayDate = getTodaysDate();
if (date > todayDate)
document.searchForm["date"].value = todayDate;
}
function getTodaysDate(){
date = new Date();
day = date.getDate();
month = date.getMonth() + 1;
year = date.getFullYear();
if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;
today = year + "-" + month + "-" + day;
return today;
}