Checking if at least one checkbox has been checked

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • uicouic
    New Member
    • Jan 2009
    • 24

    Checking if at least one checkbox has been checked

    Hi all. I need JavaScript to validate that atleast one checkbox has been checked in a form before the record(s) can be deleted.

    - I have an <asp:button id="btnDelete". ..> and the checkbox is created via grdData_ItemDat aBound with id="chkRecordId ":

    Code:
    Protected Sub grdData_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles grdData.ItemDataBound
    
            If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer   Then
                [B]e.Item.Cells(1).Text = "<input type='checkbox' name='chkRecordId' value='" & e.Item.Cells(0).Text & "'/>"[/B]
    
           End If
    End Sub
    How do I achieve this? Any help would be greatly appreciated.

    Thanks.
    Last edited by acoder; Jan 14 '09, 09:57 AM. Reason: Added [code] tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Since the checkboxes are named "chkRecordI d", you can access them using :
    Code:
    var checkboxes = document.getElementsByName("chkRecordId");
    then loop over them and check the checked property. If true, then the current checkbox (which you've reached in the loop) is checked otherwise not.

    PS. please use [code] tags around your code. Thanks!

    Comment

    • uicouic
      New Member
      • Jan 2009
      • 24

      #3
      Originally posted by acoder
      Since the checkboxes are named "chkRecordI d", you can access them using :
      Code:
      var checkboxes = document.getElementsByName("chkRecordId");
      then loop over them and check the checked property. If true, then the current checkbox (which you've reached in the loop) is checked otherwise not.

      PS. please use [code] tags around your code. Thanks!
      Ok I'll try it out. Thanks alot.

      Sorry, I'll do it next time. New here :P

      Comment

      • uicouic
        New Member
        • Jan 2009
        • 24

        #4
        By the way, can I just use:

        Code:
        if (checkboxes.checked==false) {
            strMessage += "<li>Please select minimum one competition category record  to delete<br>";

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          No, you can't. checkboxes would be an array, so you need to index it, e.g.
          checkboxes[0] to refer to the first checkbox. You can use a for loop:
          Code:
          for (i = 0; i < checkboxes.length; i++) {
              if (!checkboxes[i].checked) ...
          }

          Comment

          • uicouic
            New Member
            • Jan 2009
            • 24

            #6
            Oh ok I get it now. Thanks alot acoder. Really appreciate it.
            :)

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              No problem at all. You're welcome :)

              Comment

              • uicouic
                New Member
                • Jan 2009
                • 24

                #8
                Bummer just when I thought I could get it to work, an error propped up:
                "Object reference not set to an instance of an object."

                These two lines caused the error:
                Code:
                strRecordIds = Request.Form("chkRecordId") 
                'Use the Request.Form to collect data from checkboxes
                
                aryRecordId = strRecordIds.Split(",") 
                'Use the Split method to generate an Array, aryRecordId
                Something to do with the aryRecordId I guess as it is split via strRecordIds as shown above.

                Btw, as advised my code in the .aspx file is:

                Code:
                var checkboxes = document.getElementsByName("chkRecordId");
                
                    for (int i = 0; i < checkboxes.length; i++) {
                    if (checkboxes[i].checked != true) {
                    strMessage += "<li>Please select minimum one competition category record to delete<br>";
                    return false;
                    }
                    }
                Please help, thanks.

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Are you sure that's a JavaScript problem? Those two lines are not JavaScript code.

                  Comment

                  • uicouic
                    New Member
                    • Jan 2009
                    • 24

                    #10
                    Those two lines are found in the code-behind file of the webform where the checkboxes are found.(server-side)
                    Thus, I don't think it is a JavaScript problem. Must be a problem with the array of some sorts. (link between the code-behind file and .aspx JavaScript)

                    Hope I have provided adequate information.

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      In that case, you'll have better luck in the ASP.NET forum. Please ask there instead.

                      Comment

                      • uicouic
                        New Member
                        • Jan 2009
                        • 24

                        #12
                        Ok thanks again acoder :)

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          Just a note though before you go: your code
                          Code:
                          var checkboxes = document.getElementsByName("chkRecordId");
                          
                              for (int i = 0; i < checkboxes.length; i++) {
                              if (checkboxes[i].checked != true) {
                              strMessage += "<li>Please select minimum one competition category record to delete<br>";
                              return false;
                              }
                              }
                          will not work as desired because it's checking that all checkboxes are checked instead of at least one checkbox which could be validated as follows:
                          Code:
                          for (int i = 0; i < checkboxes.length; i++) {
                               if (checkboxes[i].checked) return true;
                          }
                          strMessage += "<li>Please select minimum one competition category record to delete<br>";
                          return false;

                          Comment

                          Working...