Client Validation (JavaScript) for CustomValidator Control

Last post 12-16-2005 4:30 PM by joteke. 1 replies.

Sort Posts:

  • Client Validation (JavaScript) for CustomValidator Control

    12-16-2005, 10:44 AM
    • Member
      200 point Member
    • g5temp
    • Member since 07-17-2003, 6:46 PM
    • Posts 41

    Hi,
    We have an ASP.NET 2.0 (C#) application that has a web form with a CheckBoxList control and a CustomValidator control.  The CustomValidator control is used to validate that at least one checkbox is checked in the CheckBoxList control.

    Everything works fine for the server-side validation.  However, we're having difficulties with writing a JavaScript function to perform the same validation on the client-side.

    Here's what we got:

    <script type="text/javascript">
        function validateChecked(oSrc, args)
        {
            args.IsValid = false;

            var formToValidate = document.forms['aspnetForm'];

            if (!formToValidate)
            {
                formToValidate = document.aspnetForm;
            }

            var controlIndex;
            var numberOfControls = formToValidate.length;
            var element;

            for (controlIndex = 0; controlIndex < numberOfControls; controlIndex++)
            {
                element = formToValidate[controlIndex];

                if (element.type == "checkbox")
                {
                    if (element.checked == true)
                    {
                        args.IsValid = true;
                    }
                }
            }
        }
    </script>

    With this code, everything seems to work fine.  The problem is that we have other checkboxes on the form that are not part of the CheckBoxList control, and this JavaScript code cannot distinguish between the checkboxes in the CheckBoxList control that we want to validate and the other checkboxes that we don't want to validate.

    Does anyone know how to write this JavaScript code to validate that at least one checkbox in our CheckBoxList control is checked, while ignoring any other checkboxes on the web form?

    Since ASP.NET assigns some funky IDs/Names when the CheckBoxList is rendered (e.g.,
    <input id="ctl00_ContentPlaceHolderMain_Daily1_CheckBoxListDaily_0" type="checkbox" name="ctl00$ContentPlaceHolderMain$Daily1$CheckBoxListDaily$0" /><label for="ctl00_ContentPlaceHolderMain_Daily1_CheckBoxListDaily_0">Sun</label>), how can you reference the correct controls by ID/Name?

    Is there a better way of doing this?

    Thanks!

     

  • Re: Client Validation (JavaScript) for CustomValidator Control

    12-16-2005, 4:30 PM
    Answer
    • All-Star
      46,040 point All-Star
    • joteke
    • Member since 06-16-2002, 11:24 AM
    • Kyro, Finland
    • Posts 6,879
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    Hi,

    you can output the ClientID of the CheckBoxList to your script (with <%=%>) and also the count of items in the CBL (with Items.Count). You are on right track that you can get that done with the IDs (yes, ASP.NET can give you the client-side ID via ClientID property of the control)

    So you know what there are items with IDs

    [cbl_clientid]_0
    [cbl_clientid]_1
    ...
    ...
    [cbl_clientid]_[itemcount -1]

    So assuming you'd had CBL for example like this:

     <asp:CheckBoxList ID="CheckBoxList1" runat="server">
                <asp:ListItem>Item 1</asp:ListItem>
                <asp:ListItem>Item 2</asp:ListItem>
                <asp:ListItem>Item 3</asp:ListItem>
            </asp:CheckBoxList>

    You could have validation script as follows to use with CustomValidator

    <script type="text/javascript" >
                function validateCBL(src,args)
                {
                    var retVal=false;
                    var cnt=<%=CheckBoxList1.Items.Count%>;
                    var cblid='<%=CheckBoxList1.ClientID%>';
                   
                    for(i=0;i<cnt;i++)
                    {
                        if(document.getElementById(cblid + '_' + i).checked)
                        {
                            retVal=true;
                            break;
                        }
                    }
                    args.IsValid=retVal;
                }
            </script>

    Note that I output the relevant information from server-side to client-side by placing <%=%>'s to the client-side script. Those are executed when page renders so they won't contradict with your server-side processing of the control(s) on the page.

    And note that you also need to implement the server-side validation as well, though that is easy task.

    Thanks,

    Teemu Keiski
    Finland, EU
Page 1 of 1 (2 items)