Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 input type date disable dates before today

Tags:

html

date

forms

Is there a way to disable dates before today in HTML5?

<input type="date">

I tried this

<input type="date" min="<?php echo $today; ?>"> 

but this works only on desktop browsers... safari mobile still allow dates prior to today.

like image 791
Salvatore Dibenedetto Avatar asked Jul 28 '16 13:07

Salvatore Dibenedetto


People also ask

How do I set the calendar to not allow dates before today's date in HTML?

toISOString(). split('T')[0]; document. getElementsByName("setTodaysDate")[0]. setAttribute('min', today);

How do I disable the date in the past date field?

the previous dates we need to set the minDate property of the date picker. if we set minDate:0 then it will disable all the previous dates. and we set input attribute min:current_date then it will disable all the previous dates.

How do I disable input type date?

< p >To disable the date field, double click the "Disable" button. // Set disabled = true.


2 Answers

Input date must be in ISO format (which is supported by mobile browsers).

There is no possible way to do this with pure HTML5.

But with Javascript, you can do something like:

<input name="setTodaysDate" type="date">

and;

var today = new Date().toISOString().split('T')[0];
document.getElementsByName("setTodaysDate")[0].setAttribute('min', today);

This little script will change the min with today's date in ISO format.

Live example here: jsFiddle

like image 182
Emre Bolat Avatar answered Nov 06 '22 03:11

Emre Bolat


var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
  dd = '0' + dd
}
if (mm < 10) {
  mm = '0' + mm
}

today = yyyy + '-' + mm + '-' + dd;
document.getElementById("datefield").setAttribute("max", today);
<input id="datefield" type='date' min='1899-01-01' max='2000-13-13'></input>
like image 40
Albin C S Avatar answered Nov 06 '22 03:11

Albin C S