locked
CustomValidator to validate dates between two Calendar controls RRS feed

  • Question

  • User1092402910 posted

    Novice developer here with a basic question.

    I have two ASP calendar controls on my aspx page. These controls present the user with an option to bring back all records between two specific dates. Everything works but I want to introduce validation to prevent users from selecting a start date that occurs after the end date. I'm aware that this can be done using a CompareValidator but I have a requirement to demonstrate this validation through using a CustomValidator.

    My question is...is this even possible to achieve with a CustomValidator? If yes, any example would be appreciated.

    I have included the code I have below although at the moment, when I launch the web application, I am met with the error: Control 'customSearchCalendarTo' referenced by the ControlToValidate property of 'valDateRange' cannot be validated.

    Many thanks.

    Code on my aspx page:

    <asp:CustomValidator runat="server" ID="valDateRange" ControlToValidate="customSearchCalendarTo" OnServerValidate="valDateRange_ServerValidate" ErrorMessage="Your date range is invalid. The FROM date cannot be after the TO date." ValidateEmptyText="True" />
    <asp:Calendar ID="customSearchCalendarFrom" runat="server" CssClass="w3TableCal" Font-Size="Medium"></asp:Calendar>
    <asp:Calendar ID="customSearchCalendarTo" runat="server" CssClass="w3TableCal" Font-Size="Medium"></asp:Calendar>

    Code in my C# code behind:

    protected void valDateRange_ServerValidate(object source, ServerValidateEventArgs args)
        {
            DateTime minDate = customSearchCalendarFrom.SelectedDate;
            DateTime maxDate = customSearchCalendarTo.SelectedDate;
            DateTime dt;
            args.IsValid = (DateTime.TryParse(args.Value, out dt))
                && dt <= maxDate
                && dt >= minDate;
        }
    Tuesday, August 8, 2017 10:23 AM

Answers

  • User-335504541 posted

    Hi AD83,

    I think you could validate two dates in calendar's OnSelectionChanged event.

    For example:

    In aspx:

                <asp:CustomValidator runat="server" ID="valDateRange"  OnServerValidate="valDateRange_ServerValidate" ErrorMessage="Your date range is invalid. The FROM date cannot be after the TO date." ValidateEmptyText="True" />
                <asp:Calendar ID="customSearchCalendarFrom" runat="server" CssClass="w3TableCal" Font-Size="Medium" OnSelectionChanged="customSearchCalendarTo_SelectionChanged"></asp:Calendar>
                <asp:Calendar ID="customSearchCalendarTo" runat="server" CssClass="w3TableCal" Font-Size="Medium" OnSelectionChanged="customSearchCalendarTo_SelectionChanged"></asp:Calendar>
                <asp:Label ID="Label1" runat="server"></asp:Label>
    

    In behind code:

            protected void customSearchCalendarTo_SelectionChanged(object sender, EventArgs e)
            {
                if (customSearchCalendarFrom.SelectedDate != DateTime.MinValue && customSearchCalendarTo.SelectedDate != DateTime.MinValue)
                    if (customSearchCalendarFrom.SelectedDate > customSearchCalendarTo.SelectedDate)
                    {
                        customSearchCalendarTo.SelectedDates.Clear();
                        Label1.Text = "Your date range is invalid. The FROM date cannot be after the TO date.";
                    }
                    else
                    {
                        Label1.Text = "";
                    }
            }

    And the result is:

    Best Regards,

    Billy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 9, 2017 8:28 AM

All replies

  • User475983607 posted

    The TryParse() method expects a single value not a collection.

    DateTime.TryParse(args.Value, out dt

    Anyway, you almost have it.

    protected void valDateRange_ServerValidate(object source, ServerValidateEventArgs args)
    {
    	args.IsValid = customSearchCalendarFrom.SelectedDate < customSearchCalendarTo.SelectedDate;
    }

    Tuesday, August 8, 2017 10:57 AM
  • User1092402910 posted

    Many thanks for your response and for your suggestion. However, I am still getting the error: Control 'customSearchCalendarTo' referenced by the ControlToValidate property of 'valDateRange' cannot be validated when attempting to run the web application. What is wrong with my code on the aspx page?

    Tuesday, August 8, 2017 1:50 PM
  • User475983607 posted

    Many thanks for your response and for your suggestion. However, I am still getting the error: Control 'customSearchCalendarTo' referenced by the ControlToValidate property of 'valDateRange' cannot be validated when attempting to run the web application. What is wrong with my code on the aspx page?

    Use a hidden field to hold the selected value of the calendar control.  Point the validation controls to the hidden filed.

    https://forums.asp.net/t/1214628.aspx?Calendar+Validator

    https://stackoverflow.com/questions/9415206/calendar-custom-validator

    Tuesday, August 8, 2017 2:01 PM
  • User-335504541 posted

    Hi AD83,

    I think you could validate two dates in calendar's OnSelectionChanged event.

    For example:

    In aspx:

                <asp:CustomValidator runat="server" ID="valDateRange"  OnServerValidate="valDateRange_ServerValidate" ErrorMessage="Your date range is invalid. The FROM date cannot be after the TO date." ValidateEmptyText="True" />
                <asp:Calendar ID="customSearchCalendarFrom" runat="server" CssClass="w3TableCal" Font-Size="Medium" OnSelectionChanged="customSearchCalendarTo_SelectionChanged"></asp:Calendar>
                <asp:Calendar ID="customSearchCalendarTo" runat="server" CssClass="w3TableCal" Font-Size="Medium" OnSelectionChanged="customSearchCalendarTo_SelectionChanged"></asp:Calendar>
                <asp:Label ID="Label1" runat="server"></asp:Label>
    

    In behind code:

            protected void customSearchCalendarTo_SelectionChanged(object sender, EventArgs e)
            {
                if (customSearchCalendarFrom.SelectedDate != DateTime.MinValue && customSearchCalendarTo.SelectedDate != DateTime.MinValue)
                    if (customSearchCalendarFrom.SelectedDate > customSearchCalendarTo.SelectedDate)
                    {
                        customSearchCalendarTo.SelectedDates.Clear();
                        Label1.Text = "Your date range is invalid. The FROM date cannot be after the TO date.";
                    }
                    else
                    {
                        Label1.Text = "";
                    }
            }

    And the result is:

    Best Regards,

    Billy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 9, 2017 8:28 AM
  • User1092402910 posted

    Many thanks Billy Liu for the detailed response. Your solution works perfectly for what I need!

    I'd also like to thank mgebhard for his time. 

    Thursday, August 10, 2017 9:03 AM