Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to know checkbox is selected?

 
Murad Iqbal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I need to do a validation check before submittion my jsp data to the servlet. I need to ensure that at least one checkbox of the checkbox array is selected. Kindly inform how to do this. I am using the following method but to no avail.

for (int i=0;i<n;i++)
{
if(document.frmReport.box[i].checked)
{
boolFlag = true;
}
}
 
Pedro Penna
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is client-side scripting. Try this:

 
Yuriy Fuksenko
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
document.frmReport.box[i].checked


Murad, that line only make sense, if all your checkboxes have the same name.
Otherwise there are 2 choices:

1. You can have a function, that goes through your checkboxes right before submitting a form, and checks does any selected. This is exactly what Pedro example is about. I wrote another version of the same function, that stops if the next element in the sequence does not exist, and therefor doesnot need to know how many checkboxes you have.


2. You actually have another choice here. Let say you have a variable, that will be incremented, when any checkbox checked and decremnted when it is unchecked. Than before form submitting you only need to test is that variable greater than zero or not. Good thing about it - it does not care about your naming patterns and how many elements you got.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic