Home Asp.net CheckBoxList Validation: Validate CheckBoxList using JavaScript in Asp.net

CheckBoxList Validation: Validate CheckBoxList using JavaScript in Asp.net

25
0
How to Validate Checkboxlist in jQuery?
How to Validate Checkboxlist in jQuery?

In my previous tutorials, I’d explained how to validate checkboxlist using jquery, validate radiobuttonlist using javascript, validate dropdownlist using javascript and other more cracking tutorials on CheckBoxList, Asp.net here.

Validation is one of the greatest security features to prevent your website against spam. We can do validation in two ways. One is from client-side another is from server-side validation. Now here in this tutorial, I’ll explain how to validate checkboxlist from client-side using JavaScript in asp.net.

Now we are going to implement the code, follow the step by step guidelines to validate the check box list.

Function To Validate Asp.net CheckBoxList Control – [JavaScript]

Here is the JavaScript function that we need to use to validate checkbox list:

<script type="text/javascript">
    function ValidateCheckBoxList() {
        var isChecked = false;
        var cblImageFormat = document.getElementById("<%=cblImageFormat.ClientID%>");
        var chkButtons = cblImageFormat.getElementsByTagName("input");

        for (var i = 0; i < chkButtons.length; i++) {
            if (chkButtons[i].checked) {
                isChecked = true;
                break;
            }
        }

        if (!isChecked) {
            alert("Please select image format from the list.");
        }

        return isChecked;
    }
</script>

HTML Markup For CheckBoxList Validation – [.aspx]

Following is the complete HTML Markup code for .aspx page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>CheckBoxList Validation using JavaScript</title>
    <script type="text/javascript">
        function ValidateCheckBoxList() {
            var isChecked = false;
            var cblImageFormat = document.getElementById("<%=cblImageFormat.ClientID%>");
            var chkButtons = cblImageFormat.getElementsByTagName("input");

            for (var i = 0; i < chkButtons.length; i++) {
                if (chkButtons[i].checked) {
                    isChecked = true;
                    break;
                }
            }

            if (!isChecked) {
                alert("Please select image format from the list.");
            }

            return isChecked;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>Select Image Format</td>
                    <td>:</td>
                    <td>
                        <asp:CheckBoxList ID="cblImageFormat" runat="server" RepeatDirection="Horizontal">
                            <asp:ListItem Value="1" Text=".jpg" />
                            <asp:ListItem Value="2" Text=".png" />
                            <asp:ListItem Value="3" Text=".gif" />
                        </asp:CheckBoxList>
                    </td>
                </tr>
                <tr>
                    <td colspan="3">&nbsp;&nbsp;</td>
                </tr>
                <tr>
                    <td colspan="2">&nbsp;&nbsp;</td>
                    <td>
                        <asp:Button ID="btnSave" OnClientClick="return ValidateCheckBoxList();"
                            runat="server" Text="Save" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

In above sample code, you can examine that I have used OnClientClick=”return ValidateCheckBoxList();” which means we are calling a ValidateCheckBoxList() function on client-click event of btnSave and evaluate that the image type is selected or not from the checkboxlist to validate checkbox or checkboxlist in asp.net.

Example Result

CheckBoxList Validation: Validate CheckBoxList using JavaScript in Asp.net
CheckBoxList Validation: Validate CheckBoxList using JavaScript in Asp.net

Download Example

Git Repo

That’s it, this is one of the simplest way to validate checkbox list from client-side using javascript or jquery in asp.net.

Let me know if you’ve any questions or doubts about this tutorial by writing down your queries in comment below. I would be happy to provide you my feedback as soon as possible.

Happy Coding!

Previous articleHow to jQuery Split String to Array in Asp.net
Next articleRadioButtonList Validation: Validate RadioButtonList using JavaScript in Asp.net
Hi there, I am Mayank, the man behind Technical Mack. I started AspnetO with a motive to educate people on various programming languages ranging from beginners to expert level. Through this blog, I aim to provide more insightful content.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

fourteen − 5 =