Click here to Skip to main content
15,844,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<tr>
                        <td height="12" align="left" class="cont">
                            <asp:CheckBox ID="chkacsoutreach" runat="server" />Acs Outreach
                        </td>

                    </tr>
                    <tr>
                        <td height="12" align="left" class="cont">
                           <asp:CheckBox ID="chkaceportal" runat="server" /> Ace Portal
                        </td>

                    </tr>


I took some checkboxes in the following way.I should get a message if atleast 1 checkbox is not checked and the values should not be entered into the database.This is the validation i needed.I have written the javascript code as below:but instead of 'controls[]' there should be a groupname which combines all the checkboxes.Iam not understanding what name to give here.Can any one please help me out


XML
<script type="text/javascript" language="javascript">
                      function validate() {
                          var chks = document.getElementsByName('controls[]');
                          var hasChecked = false;
                          for (var i = 0; i < chks.length; i++) {
                              if (chks[i].checked) {
                                  hasChecked = true;
                                  break;
                              }
                          }
                          if (hasChecked == false) {
                              alert("Please select at least one.");
                              return false;
                          }
                          return true;
                      }
                          </script>
Posted

script change a little.
<script src="jquery-1.4.1.js" type="text/javascript" language="javascript"></script>

XML
<script type="text/javascript" language="javascript">
                      function validate() {
                          var hasChecked = false;
                          var chkBox = $(':checkbox');
                          for (i = 0; i < chkBox.length; i++) {
                              if (chkBox[i].checked) {
                                  hasChecked = true;
                                  break;
                              }
                          }
                          if (!hasChecked) {
                              alert("Please select at least one.");
                              return false;
                          }
                          return true;
                      }
                          </script>

[Edit]
also check this link
:checked Selector[^]
[/Edit]
 
Share this answer
 
v2
Comments
Member 11214445 9-Apr-15 3:05am    
superb ..its worked for me
raju melveetilpurayil 10-Apr-15 6:35am    
Thank you :)
Why not put a Validator control and put all the checkboxes into one group assigned to that validator?

For now, if you want changes in JS, then have a look at this thread[^] and this [^]thread.
 
Share this answer
 
hello,

You can modify the JavaScript to something like

XML
<script type="text/javascript" language="javascript">
                     function validate() {
                         var chks = document.getElementsByTagName('input');
                         var hasChecked = false;
                         for (var i = 0; i < chks.length; i++) {
                             if (chks[i].type.toLowerCase() == "checkbox") {
                                 if (chks[i].checked) {
                                     hasChecked = true;
                                     break;
                                 }
                             }
                         }
                         if (hasChecked == false) {
                             alert("Please select at least one.");
                             return false;
                         }
                         return true;
                     }
                         </script>



source: http://stackoverflow.com/questions/838677/how-do-i-make-sure-that-at-least-one-checkbox-is-[^]
 
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