Mar 30, 2012

Required Field Validator for Checkboxlist in ASP.Net

RequiredField Validator does not directly work with CheckBoxList Control.
In this post, I am providing an alternate way to achieve the same with CustomValidator and some JQuery code

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
        function ValidateCheckBoxList(sender, args) {
            args.IsValid = false;
            jQuery(".CheckBoxList").find(":checkbox").each(function () {
                if (jQuery(this).attr("checked")) {
                    args.IsValid = true;
                    return;
                }
            });
        }
</script>

<asp:CheckBoxList ID="cblItems" runat="server" RepeatDirection="Horizontal" CssClass="CheckBoxList">
    <asp:ListItem Text="Item 1" Value="1"></asp:ListItem>
    <asp:ListItem Text="Item 2" Value="2"></asp:ListItem>
    <asp:ListItem Text="Item 3" Value="3"></asp:ListItem>
</asp:CheckBoxList>
<asp:CustomValidator ID="cvEventsValidator" Display="Dynamic" ValidationGroup="Submit"
    runat="server" ClientValidationFunction="ValidateCheckBoxList">*</asp:CustomValidator>
<br />
<asp:Button ID="btn" Text="Submit" runat="server" ValidationGroup="Submit" />

    Choose :
  • OR
  • To comment
No comments:
Write Comments