In a input type="date"
, it is possible to limit the minimum and maximum values. For example, to accept only 4-digit years, you can set the maximum value to 31 December 9999 (remembering that the date must be in the format set by ISO 8601 standard):
<input type="date" max="9999-12-31" name="data">
But in question it is said that one wants "at least the current day and at most 2 years ahead". So the minimum and maximum values are dynamic, so you should set them via Javascript. Ex:
function pad(valor) { // completa com zeros à esquerda, caso necessário
return valor.toString().padStart(2, '0');
}
function formata(data) {
return `${data.getFullYear()}-${pad(data.getMonth() + 1)}-${pad(data.getDate())}`;
}
const campo = document.querySelector('#campoData');
window.addEventListener('DOMContentLoaded', function() {
var data = new Date(); // data de hoje
campo.min = formata(data);
// 2 anos à frente
data.setFullYear(data.getFullYear() + 2);
campo.max = formata(data);
});
// mensagens de validação
campo.addEventListener('input', () => {
campo.setCustomValidity('');
campo.checkValidity();
});
// se tentar submeter o form com data fora do intervalo, mostra o erro
campo.addEventListener('invalid', () => {
campo.setCustomValidity('A data deve estar entre hoje e 2 anos à frente');
});
/* deixar borda vermelha enquanto o campo for inválido */
input:invalid {
border: red 1px solid;
}
<form>
<input id="campoData" type="date" name="data">
<input type="submit" value="ok">
</form>
According to the documentation, values should always be set in the already mentioned ISO 8601 format ("yyyy-mm-dd"), so I created the functions to assist in formatting.
Then I set the minimum and maximum values according to the given rules (minimum = current date, maximum = current date + 2 years).
Important detail: the behavior of input type="date"
is dependent on the browser. Many show a calendar when you click the field - for example, Chrome looks like this:
Note that dates prior to today are faded and nothing happens if you click on them.
But nothing prevents you from typhoon an invalid date directly in the field. So in the above example I added a CSS to make the border red if the value is invalid (just so you can see that the browser detects when the value is outside the established limits), and does not leave the form be submitted (type a date outside the allowed range and then try to click "ok").
But of course this more "visual" part would only help the user because you should also validate the date received on the server.
Finally, do not use regex for this. Regex basically handles text (same digits as 1
and 2
are treated as mere characters), so making numerical comparisons (how to know if the year is between the current and 2 years ahead) is quite complicated and not worth it (just to quote an example, see here how regex can end up getting very complicated if you really want to validate any possible date).
I believe that solves my problem temporarily. Thanks for the help, this validation is very complex for my Javascript level, so I get a little lost with some things.
– Gildevan Pereira
@Gildevanpereira If the answer solved your problem, you can accept it, see here how and why to do it. It is not mandatory, but it is a good practice of the site, to indicate to future visitors that it solved the problem. Don’t forget that you can also vote in response, if it has found it useful.
– hkotsubo
@Gildevanpereira Anyway, if the minimum and maximum values are dynamic (vary according to the current date), there is not much to escape from Javascript :-)
– hkotsubo
This code helped me a lot but now as I do to use this code for the return date field?
– Gildevan Pereira