Home > .Net | Javascript > Check at least one checkbox is selected in CheckBoxList using ASP.Net Validators

Check at least one checkbox is selected in CheckBoxList using ASP.Net Validators

I found a very good article to check that at least one chekbox is selected in a checkboxlist control. Works exactly like required field validator.

http://forums.asp.net/t/1000658.aspx (check answer from user yasserzaid)

<script type=”text/javascript”>

function ValidateRolesSelection(source, args)

{

//if your checkboxlist control is on simple asp.net page

var chkListModules = document.getElementById(‘<%= RolesCheckBoxList.ClientID %>’);

//if your checkboxlist control is inside some user control for e.g in CreateUserWizard

//var chkListModules = document.getElementById(‘<%= (CreateUserWizard1.WizardSteps[0].Controls[0].FindControl(“RolesCheckBoxList”) as CheckBoxList).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;

}

</script>

Comments are closed.

TOP