I'm trying to compare two dates coming from fields text
, turning them into object 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><inputtype="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 could be by the format that comes from input
, but I'm not sure. I've researched SOen and #, but none of the ones I saw succeeded in doing so. solve it for me.
Basically, I need when the person finishes typing (before clicking the submit button) return a alert
if the end date is less than the initial date. Any light?