How to prevent selecting date range in which there is a disabled date in between?

后端 未结 3 1137
一向
一向 2021-01-14 16:03

I have two Jquery date pickers, in which a range of dates can be selected.

I have implemented certain restrictions like, the date of textbox2 should be always greate

3条回答
  •  清歌不尽
    2021-01-14 16:28

    I added a function "validateDateRange" to your code to illustrate the logic required to complete this task. Please note that the intention of the function I added is to simply pop up an alert if the condition you described occurs. From here you should be able to do whatever you like. Let us know if you have more questions or were looking for something else.

    Here are my updates

    function validateDateRange() {
    
        var txtStartDate = $("#start_date");
        var txtEndDate = $("#end_date");
        var startDate;
        var endDate;
        var tempDate;
    
        if (txtStartDate.val() == "") 
            return false;
    
        if (txtEndDate.val() == "") 
            return false;
    
        startDate = new Date(txtStartDate.val());
        endDate = new Date(txtEndDate.val());
    
        for (i = 0; i < unavailableDates.length; i++) {
            var temp = unavailableDates[i].split("-");
    
            tempDate = new Date(temp[2], temp[1]-1, temp[0]);
    
            if (startDate < tempDate && endDate > tempDate) {
                alert("Invalid Date Range");
                return false;
            }
        }
    }
    

提交回复
热议问题