Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a group of checkboxes.
I need a validation such that atleast one checkbox must be checked or else I should get a message to select at least one checkbox.

Can any one help me out?
Posted
Updated 12-Sep-10 21:48pm
v2
Comments
Dalek Dave 13-Sep-10 3:48am    
Edited for Grammar.

Hi
You can use java script to validate it. Here is an example for three check boxes .

XML
<script type="text/javascript" language="JavaScript">
<!--
function checkCheckBoxes(theForm) {
    if (
    theForm.CHECKBOX_1.checked == false &&
    theForm.CHECKBOX_2.checked == false &&
    theForm.CHECKBOX_3.checked == false)
    {
        alert ('Select any check box!');
        return false;
    } else {
        return true;
    }
}
//-->
</script>



if you have more check boxes , use 'for' loop to iterate to find whether any one is checked or not. For this the check box names must be like a indexed one as in example

if your need is not satisfied please read this
 
Share this answer
 
v2
Comments
srujanac# 13-Sep-10 6:24am    
should i make any changes in my .aspx.cs page i.e in my code.Like to call the javascript function which i have written.
Baji Jabbar 13-Sep-10 13:14pm    
You have to add the attribute.Add property to call the javascript. On pageload add Button1.Attributes.Add("OnClick","return checkCheckBoxes(this)"
srujanac# 14-Sep-10 6:39am    
ya i got it thanks
You can use

var elements = document.getElementsByName('groupname');

for(var i=0; i< elements.length; i++)
 //check if any elements[i] is checked



:cool:
 
Share this answer
 
Comments
srujanac# 13-Sep-10 6:35am    
what should my group name be here I have taken checkboxes individually.And there is no property for checkboxes like 'name' in order to give a paticular groupname
For a Simple,
Use RequiredFieldValidator to CheckBoxList
it will throw msg if No checkbox is selected.
 
Share this answer
 
Comments
srujanac# 14-Sep-10 5:22am    
I tried but its showing me a mesg that we cannot use required field validator to checkboxlist
Hiren solanki 14-Sep-10 5:35am    
if you are using C#, then validate it at server side like
if (CheckBoxList1.SelectedIndex > 0) (it mean one of checkbox is checked )
Hiren solanki 14-Sep-10 5:37am    
sorry i am not able to edit my comment use CheckBoxList1.SelectedIndex >= 0 to ensure that atlease one checkbox is checked.
Check this thread http://www.codeproject.com/Questions/109272/validation-for-checkboxes.aspx[^]

And avoid multiple post.:mad:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900