Validate that end date is greater than start date with jQuery

前端 未结 15 1654
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 13:14

How do I check/validate in jQuery whether end date [textbox] is greater than start date [textbox]?

相关标签:
15条回答
  • 2020-11-22 13:21

    I like what Franz said, because is what I'm using :P

    var date_ini = new Date($('#id_date_ini').val()).getTime();
    var date_end = new Date($('#id_date_end').val()).getTime();
    if (isNaN(date_ini)) {
    // error date_ini;
    }
    if (isNaN(date_end)) {
    // error date_end;
    }
    if (date_ini > date_end) {
    // do something;
    }
    
    0 讨论(0)
  • 2020-11-22 13:23
    jQuery('#from_date').on('change', function(){
        var date = $(this).val();
        jQuery('#to_date').datetimepicker({minDate: date});  
    });
    
    0 讨论(0)
  • 2020-11-22 13:27

    this should work

    $.validator.addMethod("endDate", function(value, element) {
                var startDate = $('#date_open').val();
                return Date.parse(startDate) <= Date.parse(value) || value == "";
            }, "* End date must be after start date");

    0 讨论(0)
  • 2020-11-22 13:28

    First you split the values of two input box by using split function. then concat the same in reverse order. after concat nation parse it to integer. then compare two values in in if statement. eg.1>20-11-2018 2>21-11-2018

    after split and concat new values for comparison 20181120 and 20181121 the after that compare the same.

    var date1 = $('#datevalue1').val();
    var date2 = $('#datevalue2').val();
    var d1 = date1.split("-");
    var d2 = date2.split("-");
    
    d1 = d1[2].concat(d1[1], d1[0]);
    d2 = d2[2].concat(d2[1], d2[0]);
    
    if (parseInt(d1) > parseInt(d2)) {
    
        $('#fromdatepicker').val('');
    } else {
    
    }
    
    0 讨论(0)
  • 2020-11-22 13:29

    Little late to the party but here is my part

    Date.parse(fromDate) > Date.parse(toDate)

    Here is the detail:

    var from = $("#from").val();
    var to = $("#to").val();
    
    if(Date.parse(from) > Date.parse(to)){
       alert("Invalid Date Range");
    }
    else{
       alert("Valid date Range");
    }
    
    0 讨论(0)
  • 2020-11-22 13:30

    I was just tinkering with danteuno's answer and found that while good-intentioned, sadly it's broken on several browsers that are not IE. This is because IE will be quite strict about what it accepts as the argument to the Date constructor, but others will not. For example, Chrome 18 gives

    > new Date("66")
      Sat Jan 01 1966 00:00:00 GMT+0200 (GTB Standard Time)
    

    This causes the code to take the "compare dates" path and it all goes downhill from there (e.g. new Date("11") is greater than new Date("66") and this is obviously the opposite of the desired effect).

    Therefore after consideration I modified the code to give priority to the "numbers" path over the "dates" path and validate that the input is indeed numeric with the excellent method provided in Validate decimal numbers in JavaScript - IsNumeric().

    In the end, the code becomes:

    $.validator.addMethod(
        "greaterThan",
        function(value, element, params) {
            var target = $(params).val();
            var isValueNumeric = !isNaN(parseFloat(value)) && isFinite(value);
            var isTargetNumeric = !isNaN(parseFloat(target)) && isFinite(target);
            if (isValueNumeric && isTargetNumeric) {
                return Number(value) > Number(target);
            }
    
            if (!/Invalid|NaN/.test(new Date(value))) {
                return new Date(value) > new Date(target);
            }
    
            return false;
        },
        'Must be greater than {0}.');
    
    0 讨论(0)
提交回复
热议问题