validate end date equal to greater than start date

前端 未结 2 1433
清酒与你
清酒与你 2021-01-20 20:52

I want to check whether end date is greater than or equal to start date, with jquery validate. It is validating end date greater than start date, but it is not permitting en

相关标签:
2条回答
  • 2021-01-20 21:35

    You should use is greater than or equal to(>=) in your custom method greaterThan.

    Change this line:

    return new Date(value) > new Date($(params).val());
    

    To:

    return new Date(value) >= new Date($(params).val());
    

    Enjoy...

    0 讨论(0)
  • 2021-01-20 21:45

    If you want to use simple javascript to compare ethe date. Here is one; simple and easier.

     function compareDate() {
        var str = document.getElementById("start_date").value;
        var end = document.getElementById("end_date").value;
        var year = str.substring(0,4);
        var month = str.substring(5,7);
        var date = str.substring(8,10);
        var endYear = end.substring(0,4);
        var endMonth = end.substring(5,7);
        var endDate = end.substring(8,10);
        var startDate = new Date(year, month-1, date);
        var endDate = new Date(endYear, endMonth-1, endDate);
    
        if (startDate > endDate) {
          alert('start date should be less than end date');
           return false;
        } 
        else { return true; }
    }
    
    0 讨论(0)
提交回复
热议问题