Validating ASP.Net CheckBoxList using Custom Validator control.

vishalneeraj-24503
Posted by vishalneeraj-24503 under ASP.NET category on | Points: 40 | Views : 366
<asp:CheckBoxList ID = "chk_emloyee" runat = "server">
<asp:ListItem Text = "Vishal" />
<asp:ListItem Text = "Manoj" />
<asp:ListItem Text = "Kumar" />
<asp:ListItem Text = "Neeraj" />
<asp:ListItem Text = "Gautam" />
<asp:ListItem Text = "Rajesh" />
<asp:ListItem Text = "Pappi" />
</asp:CheckBoxList>

<asp:CustomValidator ID = "cstm_val_emp" ErrorMessage = "Please select at least one item."
ForeColor = "Red" ClientValidationFunction = "Validate_Emp_Chkboxlist" runat = "server" />
<br />

<asp:Button ID = "btn_submit" Text = "Submit" runat = "server" /><br />

<script type="text/javascript">
function Validate_Emp_Chkboxlist(sender, args)
{
var checkBoxList = document.getElementById("<%=chk_emloyee.ClientID %>");
var checkboxes = checkBoxList.getElementsByTagName("input");
var isValid = false;

for (var i = 0; i < checkboxes.length; i++)
{
if (checkboxes[i].checked)
{
isValid = true;
break;
}
}
args.IsValid = isValid;
}
</script>

Comments or Responses

Login to post response