1
I am trying to validate a date field of id 'txtDat', as follows:
I want that if the person informs an earlier date than today, show a alert("Por favor, insira uma data futura.")
, and if you enter a date greater than 30 days, show the alert("Por favor, insira uma data menor que 30 dias.")
also, if not, it follows.
As a base I have this code that I got here in stack overflow to validate larger and smaller 65 years
var data_ano = cad.txtDat.value;
var separa = ~data_ano.indexOf("/") ? "/" : "-";
var data_array = Number(data_ano.split(separa).filter(function(e){
return e.length == 4;
}));
var este_ano = (new Date()).getFullYear();
if(data_array > este_ano-18 || data_array < este_ano-65){
alert("É preciso ter 18 anos e menos de 65 anos.");
return;
}
I tried instead of getfullyear
the getday
, tried several other things but could not do.
https://momentjs.com - Maybe this will help you A lot!
– Vinícius Carra
And if the date matches today?
– Sam
Remembering that doing this by Javascript can easily be circumvented. Simply the User changes the system date.
– Sam
@dvd do by php then?
– Pedro Vinicius
@dvd if the date is today all right
– Pedro Vinicius
You can do it for both. In PHP it’s for real.
– Sam
then I will validate it by php anyway
– Pedro Vinicius
@dvd but could you do this function just so I could see how it would look? if I were to use it urgently
– Pedro Vinicius