RequiredFieldValidator for RadCalendar

5 posts, 0 answers
  1. Sonia
    Sonia avatar
    90 posts
    Member since:
    Mar 2012

    Posted 09 Jul 2014 Link to this post

    Hello,

    Is it posible to validate if there is any selected date in a RadCalendar?

  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 09 Jul 2014 in reply to Sonia Link to this post

    Hi Sonia,

    Please try to use CustomValidator to achieve your scenario.

    ASPX:
    <telerik:RadCalendar ID="RadCalendar1" runat="server">
    </telerik:RadCalendar>
    <asp:CustomValidator ID="CustomValidator1" runat="server" EnableClientScript="true"
        ClientValidationFunction="validateCalendar" ErrorMessage="Select Any Date">
    </asp:CustomValidator>
    <asp:Button ID="Button1" Text="Postback" runat="server" />

    JavaScript:
    function validateCalendar(sender, args) {
        var calendar = $find("<%=RadCalendar1.ClientID%>");
        if (calendar.GetSelectedDates().length == 0) {
            args.IsValid = false;
            return false;
        }
        return true;
    }

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Sonia
    Sonia avatar
    90 posts
    Member since:
    Mar 2012

    Posted 10 Jul 2014 in reply to Princy Link to this post

    Thank you Princy, it works as expected.
  5. Sonia
    Sonia avatar
    90 posts
    Member since:
    Mar 2012

    Posted 24 Oct 2014 in reply to Princy Link to this post

    Hi Princy,

    How can i validate calendar when it is inside an InsertItemTemplate of a RadGrid? I mean I do not know RadCalendar ClientID in advance.
  6. Sonia
    Sonia avatar
    90 posts
    Member since:
    Mar 2012

    Posted 28 Oct 2014 in reply to Sonia Link to this post

    I have found the solution..., I share here in case it can be useful for other people.

    In code-behind:
            protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
            {
                    RadCalendar semana = (RadCalendar)item.FindControl("Semana");
                    CustomValidator semanaValidator = (CustomValidator)item.FindControl("Semana_CustomValidator");
                    Page.ClientScript.RegisterExpandoAttribute(semanaValidator.ClientID, "ClientID", semana.ClientID, false);
            }

    Javascript:
                function validateCalendar(sender, args) {
                    var calendar = $find(sender.ClientID); //document.getElementById(sender.ClientID);
                    if (calendar.GetSelectedDates().length == 0) {
                        args.IsValid = false;
                        return false;
                    }
                    return true;
                }


Back to Top
UI for ASP.NET Ajax is Ready for VS 2017