locked
Calendar Extender RRS feed

  • Question

  • User-415416754 posted

    Hi all, I want to do something whereby there will be a starting date and ending date. Is there any way whereby I can make sure that the user cannot select the ending date that is before the starting date. This means that I must make sure that the ending date will always be more than the starting date.

    Is there any way whereby I can modify the following codes for me to show a error message whenever the user chose a ending date that is before the starting date?

    <asp:CalendarExtender ID="TextBox2_CalendarExtender" runat="server"
    Enabled="True" TargetControlID="TextBox2" Format="MM-dd-yyyy" OnClientDateSelectionChanged="dateselect"">
    </asp:CalendarExtender>
    <script>
    function dateselect(ev)
    {



    var calendarBehavior1 = $find("Calendar1");
    var d = calendarBehavior1._selectedDate;
    var now = new Date();
    calendarBehavior1.get_element().value = d.format("MM/dd/yyyy") + " "+now.format("HH:mm:ss")
    }
    </script>

    Wednesday, September 25, 2013 5:14 AM

Answers

  • User-837620913 posted

    Use a compare validator on the two textboxes to ensure TextBox2 is greater than TextBox1.

    Compare Validator: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.comparevalidator.aspx

    Example: 

    <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox2"
    ControlToCompare="TextBox1" Type="Date" ValidationGroup="Page" Operator="GreatThanEqual"
    ErrorMessage="Invalid Date Range"></asp:CompareValidator>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 25, 2013 5:24 AM

All replies

  • User839260933 posted

    Hi

    I have implemented this by the below code. Please try

    codebehind :

     private void validDetails()
        {
            string FromDate = "" ;
            string ToDate="";
            bool flag = false;
    
        
            if (txtFromDate.Text.Trim() != String.Empty && txtToDate.Text.Trim() != String.Empty)
            {
                DateTime FDate = DateTime.ParseExact(txtFromDate.Text.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
                DateTime TDate = DateTime.ParseExact(txtToDate.Text.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
    
                if (FDate.Date <= TDate.Date)
                {
                    FromDate = DateTime.ParseExact(txtFromDate.Text.Trim(), "dd/MM/yyyy", null).ToString("MM/dd/yyyy");
                    ToDate = DateTime.ParseExact(txtToDate.Text.Trim(), "dd/MM/yyyy", null).ToString("MM/dd/yyyy");
                    if (flag == false)
                    {
                        BindToGrid(FromDate, ToDate);
                    }
                    
                }
                else
                {
                    
                    RegisterStartupScript("startupScript", "<script language=JavaScript>alert( 'End Date can not be less than Start Date' );</script>");
                    txtToDate.Focus();
                }
            }
            else if (txtFromDate.Text.Trim() == String.Empty && txtToDate.Text.Trim() != String.Empty)
            {
               
                RegisterStartupScript("startupScript", "<script language=JavaScript>alert( 'Please Select Start Date' );</script>");
                txtFromDate.Focus();
            }
            else if (txtFromDate.Text.Trim() != String.Empty && txtToDate.Text.Trim() == String.Empty)
            {
                
                RegisterStartupScript("startupScript", "<script language=JavaScript>alert( 'Please Select End Date' );</script>");
                txtToDate.Focus();
            }
            else
            {
                
                RegisterStartupScript("startupScript", "<script language=JavaScript>alert( 'Please Select Dates' );</script>");
            }
        }



    Wednesday, September 25, 2013 5:23 AM
  • User-837620913 posted

    Use a compare validator on the two textboxes to ensure TextBox2 is greater than TextBox1.

    Compare Validator: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.comparevalidator.aspx

    Example: 

    <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox2"
    ControlToCompare="TextBox1" Type="Date" ValidationGroup="Page" Operator="GreatThanEqual"
    ErrorMessage="Invalid Date Range"></asp:CompareValidator>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 25, 2013 5:24 AM
  • User-415416754 posted

    Hey thanks for the reply! I did what you do to compare and it will show the error message. However, if the user choose to not follow the error and continue entering it can still enter the database. How do I stop that?

    Wednesday, September 25, 2013 6:09 AM
  • User839260933 posted

    You can try the same in code behind. I posted my fully working code above.

    Wednesday, September 25, 2013 8:10 AM