How to make the input (input field) active until a certain number (date)? Example: from 1 to 25 the number is active, and from 26 to 1 the number is not active
5 answers
<input type='text' id='time' disabled='disabled'> <script> var today_day = new Date().getDate() if (today_day >= 1 && today_day <= 25) { document.getElementById('time').removeAttribute('disabled'); } </script>
- put your code .. that's just my Input is not active, and today is the 6th number .. must be active. What is the problem, do not tell? - Sanya Sanya
- @lampa, you have an interesting condition:
today_day < 1
:) @ Sanya-Sanya, correct the condition for the right one and it will work. - Zhukov Roman - @Zhukov Roman, @ Sanya-Sanya something happened, and I confused everything. Updated post. - lampa
|
You need to read the documentation for working with dates in javascript
.
Then, on the onload
of <body>
hang up the handler that will have the necessary input'a
set the property disabled
.
|
Elementary:
var now = new Date(); var input = document.getElementById('myInput'); if (now.getDate()>=1 && now.getDate()<=25) input.disabled = false; else input.disabled = true;
|
I would do so if it is important to consider the month || year || something else besides the day.
var t = document.getElementById("t"); var now = new Date(); var block = new Date(now) ; block.setDate(8); var end_block = new Date(now); end_block.setDate(10); if(block - now <= 0 && end_block - now >= 0) { t.disabled = true; }
- oneIt may be right, but my advice for the future: never do more than what was asked. You will not get anything from this, except for lost time + support for unnecessary functions. - lampa
- > consider month || year LOL) - Palmervan
- @lampa, I spent no more than a minute on the code, so I’ll survive the loss of such time, if it would be more difficult, I wouldn’t write more than they asked;) - cyber_ua
- @Palmervan ??? - cyber_ua
- @cyber_ua well, you spent less than a minute on this code) By the way, you have an error in the operator> + - lampa
|
For some reason I can not add a comment, so I am writing in the answer. I wanted to say that all these attributes of the disabled type must be used carefully and there must be an additional check (preferably on the server). Because it is very easy to disable these attributes directly on the active "live" page and enter data even if the current date does not allow.
|