How to validate if the date is larger than the other?

Asked

Viewed 2,248 times

2

How do I validate if the date is longer than the other? I’m using .clone to duplicate the line!

 function validaData (local){
 $(local).find(".calendario").each(function (index){
      var dataInicial = new Date;
      var dataFinal = new Date;

     dataInicial = $(".data_Inicial").val();
     dataFinal = $(".data_Final").val();



     if (dataInicial >= dataFinal ) {


            alert("Data incorreta!" + dataInicial+" " + dataFinal);


     }
     else{

       alert("data Correta"+ dataInicial +" " + dataFinal);

     }

    });
}

1 answer

4

function validaDatas(){
    var dataInicial = new Date($("input[name='datainicial']").val());
    var dataFinal = new Date($("input[name='datafinal']").val());
    if (!dataInicial || !dataFinal) return false;
    if (dataInicial >= dataFinal) {
        alert("Data incorreta!");
        return false;
    } else {
        alert("Data Correta!");
        return true
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="javascript:void(0)" onsubmit="return validaDatas()">
    <label>Data inicial:</label>
    <input type="date" name="datainicial" />
    <label>Data Final:</label>
    <input type="date" name="datafinal" />
    <button>Enviar</button>
</form>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.