By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,566 Members | 1,700 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,566 IT Pros & Developers. It's quick & easy.

Checking if at least one checkbox has been checked

P: 24
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.  
  6.        End If
  7. End Sub
How do I achieve this? Any help would be greatly appreciated.

Thanks.
Jan 14 '09 #1
Share this Question
Share on Google+
12 Replies


acoder
Expert Mod 15k+
P: 16,027
Since the checkboxes are named "chkRecordId", you can access them using :
Expand|Select|Wrap|Line Numbers
  1. 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!
Jan 14 '09 #2

P: 24
@acoder
Ok I'll try it out. Thanks alot.

Sorry, I'll do it next time. New here :P
Jan 14 '09 #3

P: 24
By the way, can I just use:

Expand|Select|Wrap|Line Numbers
  1. if (checkboxes.checked==false) {
  2.     strMessage += "<li>Please select minimum one competition category record  to delete<br>";
  3.  
Jan 14 '09 #4

acoder
Expert Mod 15k+
P: 16,027
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:
Expand|Select|Wrap|Line Numbers
  1. for (i = 0; i < checkboxes.length; i++) {
  2.     if (!checkboxes[i].checked) ...
  3. }
Jan 14 '09 #5

P: 24
Oh ok I get it now. Thanks alot acoder. Really appreciate it.
:)
Jan 14 '09 #6

acoder
Expert Mod 15k+
P: 16,027
No problem at all. You're welcome :)
Jan 14 '09 #7

P: 24
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:
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
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:

Expand|Select|Wrap|Line Numbers
  1. var checkboxes = document.getElementsByName("chkRecordId");
  2.  
  3.     for (int i = 0; i < checkboxes.length; i++) {
  4.     if (checkboxes[i].checked != true) {
  5.     strMessage += "<li>Please select minimum one competition category record to delete<br>";
  6.     return false;
  7.     }
  8.     }
  9.  
Please help, thanks.
Jan 19 '09 #8

acoder
Expert Mod 15k+
P: 16,027
Are you sure that's a JavaScript problem? Those two lines are not JavaScript code.
Jan 19 '09 #9

P: 24
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.
Jan 21 '09 #10

acoder
Expert Mod 15k+
P: 16,027
In that case, you'll have better luck in the ASP.NET forum. Please ask there instead.
Jan 22 '09 #11

P: 24
Ok thanks again acoder :)
Jan 22 '09 #12

acoder
Expert Mod 15k+
P: 16,027
Just a note though before you go: your code
Expand|Select|Wrap|Line Numbers
  1. var checkboxes = document.getElementsByName("chkRecordId");
  2.  
  3.     for (int i = 0; i < checkboxes.length; i++) {
  4.     if (checkboxes[i].checked != true) {
  5.     strMessage += "<li>Please select minimum one competition category record to delete<br>";
  6.     return false;
  7.     }
  8.     }
  9.  
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:
Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < checkboxes.length; i++) {
  2.      if (checkboxes[i].checked) return true;
  3. }
  4. strMessage += "<li>Please select minimum one competition category record to delete<br>";
  5. return false;
Jan 22 '09 #13

Post your reply

Sign in to post your reply or Sign up for a free account.