0

I am using datepicker to select date in datalist control in asp.net. The scenario is the user selects the date for 2 textboxes and then click on the export to excel button , then the system generates the excel file with the data within the given dates.The issue is I want to restrict the user to select the date before clicking on export button, and the 2nd date selected should be greater than the first date selected using Jquery , below is the code:

<asp:Button runat="server" ID="btnExportbwDates" 
    Text="Export between Dates" onclick="btnExportbwDates_Click" />
    <asp:TextBox runat="server" ID="txtDateRangeOne" CssClass="txtDateRangeOne"></asp:TextBox>
    <asp:TextBox runat="server" ID="txtDateRangeTwo" CssClass="txtDateRangeTwo"></asp:TextBox>

     <script type="text/javascript">
    $(document).ready(function () {

        var pickerOpts = {
            dateFormat: "dd/mm/yy"

        };

        $(".txtDateRangeOne").datepicker(pickerOpts);
        $(".txtDateRangeTwo").datepicker(pickerOpts);

    });
</script>

2 Answers 2

1
    $(document).ready(function () {
            var dates = $(".txtDateRangeOne, .txtDateRangeTwo").datepicker({
                showOtherMonths: true,
                selectOtherMonths: true,
                showOn: "both",
                showAnim: "slide",
                showButtonPanel: true,
                changeMonth: true,
                changeYear: true,
                numberOfMonths: 2,
                buttonImageOnly: true,
                onSelect: function (selectedDate) {
                    var option = this.className.indexOf("txtDateRangeOne") >= 0 ? "minDate" : "maxDate";
                    instance = $(this).data("datepicker"),
                        date = $.datepicker.parseDate(
                            instance.settings.dateFormat ||
                            $.datepicker._defaults.dateFormat,
                            selectedDate, instance.settings);
                    dates.not(this).datepicker("option", option, date);
                }
            });
});
Sign up to request clarification or add additional context in comments.

Comments

1

Try this.

$(document).ready(function () {
    $(".txtDateRangeOne").datepicker({
        dateFormat: "dd/mm/yy"
        onSelect: function(date){
             //Once you select first date set this date as  the minDate 
             //of second datepicker 
             $(".txtDateRangeTwo" )
             .datepicker({ minDate: new Date(date) });
        }  
    });
    $(".txtDateRangeTwo").datepicker({ dateFormat: "dd/mm/yy" });

    $('#btnExportbwDates').click(function(){
        var startDate = $('.txtDateRangeOne').datepicker("getDate");
        var endDate = $('.txtDateRangeTwo').datepicker("getDate");
        if(!startDate){
            alert('Select start date');
            return false;
        }
        if(!endDate){
            alert('Select end date');
            return false;
        }
        if(startDate > endDate){
            alert('Select valid date range');
            return false;
        }

        return true;
    });
});

1 Comment

how to show the validate messages for range and empty textbox?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.