In this article I will explain how to validate CheckBoxes inside Repeater and perform at least one checked validation using JavaScript in ASP.Net.
Validation of CheckBoxes inside Repeater is performed using ASP.Net CustomValidator with a client side JavaScript validation function.
 
HTML Markup
The HTML Markup consists of an ASP.Net Repeater which renders as an HTML Table with three columns and the first column having a CheckBox placed inside it.
Below the Repeater there’s a CustomValidator and a Button control.
<asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
        <table id="tblContacts" border="0" cellpadding="0" cellspacing="0">
            <tr>
                <th>
                </th>
                <th>
                    Name
                </th>
                <th>
                    Country
                </th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <asp:CheckBox Text="" runat="server" />
            </td>
            <td>
                <%#Eval("Name") %>
            </td>
            <td>
                <%#Eval("Country") %>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please select at least one record."
    ClientValidationFunction="Validate" ForeColor="Red"></asp:CustomValidator>
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" />
 
 
Namespaces
You will need to import the following namespaces.
using System.Data;
 
 
Binding the Repeater control
I have created a dynamic DataTable with some dummy data and it has been bind to the Repeater control in Page Load event.
You can learn more about this technique in my article Create DataTable dynamically and bind to GridView in ASP.Net.
 
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                        new DataColumn("Name", typeof(string)),
                        new DataColumn("Country",typeof(string)) });
        dt.Rows.Add(1, "John Hammond", "United States");
        dt.Rows.Add(2, "Mudassar Khan", "India");
        dt.Rows.Add(3, "Suzanne Mathews", "France");
        dt.Rows.Add(4, "Robert Schidner", "Russia");
        Repeater1.DataSource = dt;
        Repeater1.DataBind();
    }
}
 
 
CustomValidator JavaScript Validation function
Below is the JavaScript function that gets executed when the CustomValidator is triggered on the button click. It first gets the HTML Table tblContacts within the Repeater and its internal CheckBoxes and then a loop is executed to check whether the CheckBoxes are checked or unchecked.
If a CheckBox is found checked then the IsValid variable is set to true else it is set to false.
<script type="text/javascript">
    function Validate(sender, args) {
        var gridView = document.getElementById("tblContacts");
        var checkBoxes = gridView.getElementsByTagName("input");
        for (var i = 0; i < checkBoxes.length; i++) {
            if (checkBoxes[i].type == "checkbox" && checkBoxes[i].checked) {
                args.IsValid = true;
                return;
            }
        }
        args.IsValid = false;
    }
</script>
 
Validate Repeater with CheckBox (at least one checked) using JavaScript in ASP.Net
 
Demo
 
Downloads