How to validate if the date is greater than the other? [duplicate]

0

How to validate if the date is greater 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);

     }

    });
}
    
asked by anonymous 22.08.2017 / 17:28

1 answer

1

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><formaction="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>
    
22.08.2017 / 17:44