简体   繁体   English

如何在日期选择器上键入后验证日期范围

[英]how to validate date range after typing on a datepicker

I have two datepickers. 我有两个日期选择器。 one is a start date and one is an end date. 一个是开始日期,一个是结束日期。 Now I can detect whether the start date is indeed earlier than the end date. 现在我可以检测开始日期是否确实早于结束日期。 And the datepickers and validations work fine as long as the user selects a date on the calendar. 只要用户在日历上选择日期,日期选择器和验证就可以正常工作。 My problem lies when I start typing a date on the End date. 我的问题在于我在结束日期开始输入日期。 For example, the start date field is May 02 2019 with the format 2019-05-02. 例如,开始日期字段是2019年5月2日,格式为2019-05-02。 Now when I start typing on the end date field let's say 2 in the value 2019, the alert box doesnt wait for me to type the whole date input and notifies me that the end date shouldnt be earlier than the start date. 现在当我开始在结束日期字段上输入时,让我们在值2019中说2,警告框不会等我输入整个日期输入并通知我结束日期不应早于开始日期。 is there any workaround for this regarding the code's logic other than a band-aid solution of converting the datepicker fields to readonly? 除了将datepicker字段转换为readonly的创可贴解决方案之外,有没有关于代码逻辑的解决方法?

here's the code for the end date: 这是结束日期的代码:

$('.endDate').each(function (index) 
{
    $(this).attr("id", "pmed" + index);
    $(this).datepicker({
        format: 'yyyy-mm-dd'
    }).on('changeDate', function (endDate) 
    {

          // Do something
          let newEnddate = new Date(endDate.date)
          newEnddate.setDate(newEnddate.getDate() + 1)
          let start_date = parseInt((new Date($("#pmst" + index).val())) / (1000 * 60 * 60 * 24))
          let end_date = parseInt(newEnddate / (1000 * 60 * 60 * 24))
          var inputCTR = parseInt(index) + 1
          if (start_date > end_date) {
            //alert("Starting date must not be less than End date.")
                    $(this).val("")
                    $('#pmst' + inputCTR).prop('disabled', true);
                    $('#pmed' + inputCTR).prop('disabled', true);
                    start_date = "NaN"
                }
                start_date = start_date.toString()
                if (start_date != "NaN" && end_date != "") {
                    $('#pmst' + inputCTR).prop('disabled', false);
                    $('#pmed' + inputCTR).prop('disabled', false);
                }


    });

});

HTML5 has a new input type date for which you don't need to use a script for validation. HTML5有一个新的输入类型日期,您不需要使用脚本进行验证。 The max and min attribute specify the range through HTML. maxmin属性通过HTML指定范围。

<input type="date" max="maxDate" min="minDate">

As you said, the validator is not waiting for you, so in order to make it wait use debounce. 如你所说,验证器不是在等你,所以为了让它等待使用去抖。 If you dont know what debounce is read it here. 如果您不知道什么是去抖动,请在此处阅读。 https://www.geeksforgeeks.org/debouncing-in-javascript/ https://www.geeksforgeeks.org/debouncing-in-javascript/

Example

$('.endDate').each(function (index){
        $(this).attr("id", "pmed" + index);
        $(this).datepicker({
            format: 'yyyy-mm-dd'
        }).on('changeDate', $.debounce(1000, function (endDate) { //1000 is in milliseconds  
            // Do something
            let newEnddate = new Date(endDate.date)
            newEnddate.setDate(newEnddate.getDate() + 1)
            let start_date = parseInt((new Date($("#pmst" + index).val())) / (1000 * 60 * 60 * 24))
            let end_date = parseInt(newEnddate / (1000 * 60 * 60 * 24))
            var inputCTR = parseInt(index) + 1
            if (start_date > end_date) {
                //alert("Starting date must not be less than End date.")
                $(this).val("")
                $('#pmst' + inputCTR).prop('disabled', true);
                $('#pmed' + inputCTR).prop('disabled', true);
                start_date = "NaN"
            }
            start_date = start_date.toString()
            if (start_date != "NaN" && end_date != "") {
                $('#pmst' + inputCTR).prop('disabled', false);
                $('#pmed' + inputCTR).prop('disabled', false);
            }
        }));
    });

There could be some coding errors as i have not run it myself but debounce is a perfect use case for you. 可能会有一些编码错误,因为我自己没有运行它,但debounce是一个完美的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM