The <input type="date" />
element has the min
and max
attributes which can be used to specify an optional minimum and maximum date range. The value must be specified in ISO-8601 format (yyyy-MM-dd
):
<input type="date" min="2016-10-01" max="2017-01-01" />
There doesn't seem to be a magic value such as "today"
, so you must set the value either on the server-side or using JavaScript:
window.addEventListener('DOMContentLoaded', restrictDateInputs );
function restrictDateInputs(e) {
var today = ( new Date() ).toISOString().substring( 10 );
var inputs = document.querySelectorAll('input[type=date].minToday');
for( var i = 0; i < inputs.length; i++ ) {
inputs[i].min = today;
}
}
The above code sets the min
attribute value on page-load for all date inputs with class="minToday"
. Adjust as per your requirements.
Remember that client-side logic will not guarantee that invalid data will not be sent to any data-processing code (either client-side or server-side), so always sanitize, validate and verify (in that order) any input you receive from a user.
Note that <input type="date" />
is still not fully supported by all desktop browsers, in particular Internet Explorer 11 and even recent versions of Firefox.