Connecting Tech Pros Worldwide Help | Site Map

Checking if atleast one checkbox has been checked using JavaScript

Newbie
 
Join Date: Jan 2009
Posts: 23
#1: Jan 22 '09
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_ItemDataBound with id="chkRecordId":

Expand|Select|Wrap|Line Numbers
  1. Protected Sub grdData_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles grdData.ItemDataBound
  2.  
  3.         If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
  4.             e.Item.Cells(1).Text = "<input type='checkbox' name='chkRecordId' value='" & e.Item.Cells(0).Text & "'/>"
  5.         End If
  6.  
  7. End Sub
As advised in another forum, I have the following JS code:

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" event="onclick" for="btnDelete">
  2.  function validateCheckboxes() {
  3.     var blnAllOkay = new Boolean(true);
  4.     var strMessage = new String("");
  5.     var checkboxes = document.getElementsByName("chkRecordId");
  6.  
  7.     for (var i = 0; i < checkboxes.lengths; i++) {
  8.     if (checkboxes[i].checked == false) {
  9.     strMessage += "<li>Please select minimum one competition category record to delete<br>";
  10.     blnAllOkay = false;
  11.     }
  12.     }
  13.  
  14.     if (blnAllOkay != true) {
  15.     window.event.returnValue=false;
  16.     lblMessage.innerHTML = strMessage;
  17.     }
  18.     }
  19. </script>
It is still unable to work though, after calling the function via btnDelete.
Error has something to do with the array String.

Please advise, thanks.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#2: Jan 22 '09

re: Checking if atleast one checkbox has been checked using JavaScript


Please post the error message you're getting.

This wont crash your JavaScript but, you are attempting to concatenate list items to your string:
Expand|Select|Wrap|Line Numbers
  1. strMessage += "<li>Please select minimum one competition category record to delete<br>";
You are not closing the list item though..
Newbie
 
Join Date: Jan 2009
Posts: 23
#3: Jan 23 '09

re: Checking if atleast one checkbox has been checked using JavaScript


The error message: "Object reference not set to an instance of an object."
The two lines in particular I feel are

Expand|Select|Wrap|Line Numbers
  1. strRecordIds = Request.Form("chkRecordId") 
  2. 'Use the Request.Form to collect data from checkboxes
  3.  
  4. aryRecordId = strRecordIds.Split(",") 
  5. 'Use the Split method to generate an Array, aryRecordId
About the list item, I have no problems so far using it for other functions, but taking into account your advice, I'll close it.

Hope I have provided enough relevant information. Thanks.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#4: Jan 23 '09

re: Checking if atleast one checkbox has been checked using JavaScript


Oh, ok. Next time you have an error please specify whether its occurring client side or server side. I didn't realize your problem was happening on the server.


Anyways, I'm not sure what you're trying to accomplish in your server side code here. You're probably getting the exception because the following line is returning null/nothing:
Expand|Select|Wrap|Line Numbers
  1. strRecordIds = Request.Form("chkRecordId") 
And then you are attempting to use the Split() method on null/nothing.

Instead of attempting to retrieve the Checked CheckBoxes using Request.Form you should loop through the GridView, using the FindControl("chkRecordId") to retrieve the check box for each row, and create your array server side (since you're using it server side).


For example:
Expand|Select|Wrap|Line Numbers
  1. Dim dr As GridViewRow
  2. Dim itemsToDelete As List(Of Integer)
  3.  
  4. For Each dr In myGridView.Rows
  5.    Dim chkBox As CheckBox = CType(dr.FindControl("chkRecordId"), CheckBox)
  6.     If chkBox.Checked = True Then
  7.          Dim x As String = dr.Cells(1).Text 'grab the cell with the aryRecordId in it
  8.          Dim aryRecordId As Integer = Integer.Parse(x)
  9.          itemsToDelete.Add(aryRecordId)
  10.     End If
  11. Next
  12.  
Newbie
 
Join Date: Jan 2009
Posts: 23
#5: Jan 28 '09

re: Checking if atleast one checkbox has been checked using JavaScript


Oh thanks alot Frinavale! Its working now.

Cheers. :)
Reply

Tags
checkbox, checked, delete, javascript, validate