5
I’m trying to compare two dates from fields text
, turning them into objects Date
, as follows (the first function is to format the field input
with XX/XX/XXXX
):
function formatar(mascara, documento){
var i = documento.value.length;
var saida = mascara.substring(0,1);
var texto = mascara.substring(i);
if (texto.substring(0,1) != saida){
documento.value += texto.substring(0,1);
}
}
if(new Date('#datafinal') <= new Date('#datainicial'))
{
alert("A data inicial é maior que a data final");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" maxlength="10" placeholder="dd/mm/aaaa"
OnKeyPress="formatar('##/##/####', this)" id="datainicial">
<br>
<input type="text" maxlength="10" placeholder="dd/mm/aaaa"
OnKeyPress="formatar('##/##/####', this)" id="datafinal">
But it’s not working. I think it might be because of the format that comes from input
, but I’m not sure. I did a search on Soen and there are several questions with many answers, but none of the ones I saw could solve for me.
Basically, I need when the person finishes typing (before clicking send button) to return a alert
if the final date is less than the initial one. Some light?
This script really helped me, thanks @Sergio.
– adventistapr