Required Field Validator for CheckBoxList

When you use asp check box list and want to apply required field validator then follow below example.
  
<asp:CheckBoxList ID="chkDemoList" runat="server" >
<asp:ListItem Text="India" Value="IN"></asp:ListItem>
<asp:ListItem Text="United States of Americs" Value="USA"></asp:ListItem>
<asp:ListItem Text="Japan" Value="JP"></asp:ListItem> 
</asp:CheckBoxList>
 
<asp:CustomValidator runat="server" ID="cvDemoList"
  ClientValidationFunction="ValidateDemoList"
  ErrorMessage="Required Field" ></asp:CustomValidator>
 
// javascript to add to your aspx page
function ValidateDemoList(source, args)
{
  var chkListModules= document.getElementById ('<%= chkDemoList.ClientID %>');
  var chkListinputs = chkListModules.getElementsByTagName("input");
  for (var i=0;i<chkListinputs .length;i++)
  {
    if (chkListinputs [i].checked)
    {
      args.IsValid = true;
      return;
    }
  }
  args.IsValid = false;
}

Comments

  1. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article.
    dot net training and placement in chennai |
    dot net course fees

    ReplyDelete
  2. CheckBoxList client side validation in Radgrid is possible..?

    ReplyDelete

Post a Comment

Popular posts from this blog

C# Copy files from one server to another

Export SharePoint Online Group Users to Excel using Powershell