kratos
kratos

Reputation: 101

Specify only YEAR and MONTH for input date min/max attributes

i have the following:

<input type="date" max="2014-10-31">

Can i specify only the year and the month? I guess it'd be like:

<input type="date" max="2014-10-xx">

or just

<input type="date" max="2014-10">

(assuming the format is yyyy/mm/dd).

And HTML does the rest. I can't specific the day, only year and month (given by server-side), because there are different max days for each month (like february, which has 28). So, can not be 31 the max.

when i put 31 [day] for max attribute, the months that have 28, 30 stop working, i mean, the input gets weird and so i can modify the month too, but it can't happen cause the user should be able only to change the day - the month and the days are specific.

Upvotes: 4

Views: 13871

Answers (3)

Rajeev Jayaswal
Rajeev Jayaswal

Reputation: 1501

Yes, you can use like following:

Date(day/month/year): <input type="date" value="2017-07-20" min="2000-01-01" max="2020-01-01">
<hr />
Date(Month/Year): <input type="month" value="2017-07" min="2000-01" max="2020-01">
<hr />
Date(year): <input type="number" value="2017" min="2000" max="2020">

Live demo: https://jsfiddle.net/2fupfqgz/2/

Upvotes: 0

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201588

No, you cannot do that with HTML. The max value in in <input type="date" ...> must by definition be a specific date, and it must be a valid date.

You should calculate the last day of the intended month when you generate the HTML markup.

Upvotes: 1

Oriol
Oriol

Reputation: 288120

You could use something like this:

<input class="month-date" type="date" max="2014-02">
var els = document.getElementsByClassName("month-date");
for(var i=0, l=els.length; i<l; ++i) {
    var max = els[i].getAttribute('max').split('-');
    max.push(new Date(max[0], max[1], 0).getDate());
    els[i].max = max.join('-');
}

Demo

Upvotes: 2

Related Questions