473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Checking if at least one checkbox has been checked

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
12 10076
acoder
16,027 Expert Mod 8TB
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
uicouic
24
@acoder
Ok I'll try it out. Thanks alot.

Sorry, I'll do it next time. New here :P
Jan 14 '09 #3
uicouic
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
16,027 Expert Mod 8TB
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
uicouic
24
Oh ok I get it now. Thanks alot acoder. Really appreciate it.
:)
Jan 14 '09 #6
acoder
16,027 Expert Mod 8TB
No problem at all. You're welcome :)
Jan 14 '09 #7
uicouic
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
16,027 Expert Mod 8TB
Are you sure that's a JavaScript problem? Those two lines are not JavaScript code.
Jan 19 '09 #9
uicouic
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
16,027 Expert Mod 8TB
In that case, you'll have better luck in the ASP.NET forum. Please ask there instead.
Jan 22 '09 #11
uicouic
24
Ok thanks again acoder :)
Jan 22 '09 #12
acoder
16,027 Expert Mod 8TB
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

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

Similar topics

5
by: NotGiven | last post by:
I have a form with several questions. Within each question there are several checkboxes. I need to ensure that the user checks at least one checkbox. They can check more but must check at least...
3
by: Wendy S | last post by:
I have this working, but I don't think it's done efficiently or the best way: <a href="javascript:setAllAccounts(true)">check all</a> <a href="javascript:setAllAccounts(false)">clear all</a> ...
3
by: Rich | last post by:
I have a form with 2 check boxes. One of the check boxes is used to specify that the user is a "primary contact." When I check the primary contact box I want a second box for "standard contact"...
2
by: Menelty | last post by:
I have 2 datagrids on my page and each one has a template column which has a checkbox relating to each specific row. I usually have one on the page and use javascript to make sure there has been x...
13
by: aundro | last post by:
Hello, I've been looking on the web for a solution to this problem: I create a set of checkboxes, and 2 buttons: - one is labeled "All" - the other is labeled "None" Clicking "All" is...
3
by: Moe Sizlak | last post by:
Hi, Can I check a checkbox by default based on the value returned from a database? for example if the value (dr("frm7value")) returned from a db field is "YES" how can I check the checkbox by...
1
by: Ramakrishnan Nagarajan | last post by:
Hi, I have two checkboxes in each row of a grid. One for Modify and another one for View. If I click Modify the View should get automatically checked and should be disabled. Earlier I did this in...
1
by: DoomedLung | last post by:
Hey, I'm currently developing an online work brief, where the user has to enter details regarding the project into a form. I've come to a section in the form where the user has the option of...
4
by: uicouic | last post by:
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.