how to do it in input type = date so that we are right now in 2018 and in this input you could only select the date until 2018, if 2019 until 2019, I think the meaning is clear. Is it possible to do this using php or js?
1 answer
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date
До 2018 года:<br/> <input type="date" max="2017-12-31"> <br/> До конца 2018 года:<br/> <input type="date" max="2018-12-31"> var editDate = document.getElementById("test"); editDate.setAttribute("max", (new Date()).getFullYear() + "-12-31"); Aвтоматически<br/> <input type="date" id="test"> function updateMinMax() { var editDate = document.getElementById("test"); editDate.value = ""; editDate.min = ""; editDate.max = ""; var schoolYear = document.getElementById("schoolYear"); var yearText = schoolYear.options[schoolYear.selectedIndex].textContent; var parts = yearText.split("/"); if (parts.length == 2) { editDate.min = parts[0] + "-09-01"; editDate.max = parts[1] + "-05-31"; } console.log("updateMinMax", editDate.min, editDate.max); } updateMinMax(); function checkDate(el) { console.log("checkDate", el.min, el.max, el.value); if (el.value < el.min || el.value > el.max) { el.value = ""; console.error("out of range"); } } По учебному году:<br/> <select id="schoolYear" onchange="updateMinMax()"> <option value="0">Select a year</option> <option value="1">2016/2017</option> <option value="2">2017/2018</option> <option value="3">2018/2019</option> </select> <input type="date" id="test" onblur="checkDate(this)"> - Thank. You answer so quickly, I did not have time to correct the question, so let's say I have a database, and in the database there is a table "academic year" with the values 2017/2018, 2018/2019. how to do that if we chose 2017/2018 using select with date type, it set the Min and max appropriate without reloading the page. I understand this ajax - Odium
- gives error schoolYear is null, can't access property "value" of it - Odium
- @ Odium Perhaps the script is in the page above the element that is searched by
id. Move the script to the bottom of the page or wrap it in awindow.onloadhandler. - Igor - It worked, I thought, there are simply no values, because the value of optin is not spelled out. Can I do a question? I get that if you take from the database of the year, then they have the same <option value = 1> 2016/2017 </ option>, <option value = 2> 2017/2018 </ option>, where value is His ID in the database, how to do that would take option not by value, but by the contents of otpion. And how to do that would make a mistake if we manually entered 2019, when we have a maximum of 2018. Thanks for the help. - Odium
- Cool, earned, Thank you. But again, I can manually enter data that does not correspond to the specified Min and max, or rather, not using the mouse and the calendar selection, but by entering numbers - Odium
|