Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
Hi frnds,

checkbox validation in asp.net C# or Javascript

I have 10 checkboxes on my webpage, from these atleast 3 checkboxes must be selected
User can select checkboxes in sequential order.

I need validation on submit button click.
(atleast 3 checkbox must be selected).

My checkboxes are from Checkbox1 to Checkbox10

Please can you help me.

Thanks.
Posted
Comments
DamithSL 9-Dec-14 5:08am    
please update the question with html code and javascript which you already tried

C#
<script type="text/javascript">
       $(document).ready(function () {
           $("#btnSubmit").click(function () {
               if($('input[type=checkbox]:checked').length<3)
               {
                   //give error message
                   return false;
               }
             //this code will get executed if atleast 3 checkboxes are checked
           });
       });
   </script>

You can use this code inside function also. Onclientclick of button,call that function and proceed with button click accordingly.

Regards...
 
Share this answer
 
v2
Comments
Praveen Kumar Upadhyay 9-Dec-14 5:57am    
This will not check for sequencial checkbox selected. Rest is perfectly fine.
Below is the Javascript code which will check for at least 3 checkbox selected in sequence.

C#
function ValidateCheckboxes() {
            var inputElems = document.getElementsByTagName("input"),
            count = 0;
            for (var i = 0; i < inputElems.length; i++)
            {
                if (inputElems[i].type === "checkbox" && inputElems[i].checked === true)
                {
                    ++count;
                }
                else if (inputElems[i].type === "checkbox" && inputElems[i].checked === false)
                {
                    //This break section will ensure that checkboxes are selected in sequence  
                    break;
                }
            }

            if (count < 3) {
                alert("Please select at least 3 checkbox in sequence");
                return false;
            }

            return true;

        }


Aspx code

XML
<div id="checkboxes">
           <asp:CheckBox ID="CheckBox1" Text="1" runat="server" />
           <asp:CheckBox ID="CheckBox2" Text="2" runat="server" />
           <asp:CheckBox ID="CheckBox3" Text="3" runat="server" />
           <asp:CheckBox ID="CheckBox4" Text="4" runat="server" />
           <asp:CheckBox ID="CheckBox5" Text="5" runat="server" />
           <asp:CheckBox ID="CheckBox6" Text="6" runat="server" />
           <asp:CheckBox ID="CheckBox7" Text="7" runat="server" />
           <asp:CheckBox ID="CheckBox8" Text="8" runat="server" />
           <asp:CheckBox ID="CheckBox9" Text="9" runat="server" />
           <asp:CheckBox ID="CheckBox10" Text="10" runat="server" />
       </div>
       <asp:Button Text="CheckboxCheck" ID="btn_checkboxCheck" runat="server" OnClientClick="return ValidateCheckboxes();" />
   </div>
 
Share this answer
 
v2
Comments
Thanks7872 9-Dec-14 5:44am    
A single line of code $('input[type=checkbox]:checked').length will give you total number of checked checkboxes.
Praveen Kumar Upadhyay 9-Dec-14 5:54am    
Agreed. But he wants checkboxes to be selected in sequence. This single line will return count but won't check for sequence.
Thanks7872 9-Dec-14 5:57am    
He has no where stated that he need code to verify the sequence.

- I have 10 checkboxes on my webpage, from these atleast 3 checkboxes must be selected
- I need validation on submit button click(atleast 3 checkbox must be selected).

He just stated that user can...
Thanks7872 9-Dec-14 6:20am    
Why you are posting question content here?
Praveen Kumar Upadhyay 9-Dec-14 6:22am    
Because you are telling that OP has no where mentioned that he wants checkboxes to be select in sequential order
hi,

at the clicking of submit btn take a variable that hold the count of selected ckeckbox.

as variable a

a=0
if (checkbox1.checked=true)
a=a+1

VB
if (checkbox2.checked=true)
a=a+1

......

....

VB
if (checkbox10.checked=true)
a=a+1


if a>=3{
true
}

else
false
 
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