locked
Calendar Validator RRS feed

  • Question

  • User1768688217 posted

    Hi, is it possible to have a validator on a calendar. I only need a required field validator but not sure how to apply one to a calendar

    Saturday, February 2, 2008 9:16 PM

Answers

  • User1672132137 posted

    I think to validate a calendar control, you can insert a hidden field control and once the user selects a date you can by javascript set the selected date to the hidden field value and the requiredfield validator will be targeted to the hidden field

    HC 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 3, 2008 10:46 AM
  • User2022958948 posted

    Hi,

    You want that the customer must click one date, otherwise, it will not be passed. You can follow the below codes which are implemented by using CustomValidator.

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (Calendar1.SelectedDate == null || Calendar1.SelectedDate == new DateTime(0001,1,1,0,0,0))// not click any date
                args.IsValid = false;
            else
                args.IsValid = true;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                //validate successfully
            }
        }
      HTML:
            <asp:Calendar ID="Calendar1" runat="server" Height="72px" Width="156px">
            </asp:Calendar>
            <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
            <asp:CustomValidator id="CustomValidator1" runat="server" Display="Dynamic" ErrorMessage="please click a day"  OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
    

     

     Hope it helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 4, 2008 5:48 AM

All replies

  • User1672132137 posted

    I think to validate a calendar control, you can insert a hidden field control and once the user selects a date you can by javascript set the selected date to the hidden field value and the requiredfield validator will be targeted to the hidden field

    HC 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 3, 2008 10:46 AM
  • User2022958948 posted

    Hi,

    You want that the customer must click one date, otherwise, it will not be passed. You can follow the below codes which are implemented by using CustomValidator.

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (Calendar1.SelectedDate == null || Calendar1.SelectedDate == new DateTime(0001,1,1,0,0,0))// not click any date
                args.IsValid = false;
            else
                args.IsValid = true;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                //validate successfully
            }
        }
      HTML:
            <asp:Calendar ID="Calendar1" runat="server" Height="72px" Width="156px">
            </asp:Calendar>
            <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
            <asp:CustomValidator id="CustomValidator1" runat="server" Display="Dynamic" ErrorMessage="please click a day"  OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
    

     

     Hope it helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 4, 2008 5:48 AM
  • User-1944934663 posted

     

    yes u can use custom validator to any other controls in asp.net

     

     

    http://dflorence25.blogspot.com

    Monday, February 4, 2008 6:00 AM