• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

How to validate that in each row how many checkboxes are checked

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have JSP page in which I have a table with rows and columns. Every row has 5 or 6 columns( all the columns are checkboxes.. and the values are generated dynamically.)
My question is how can I validate that in each row atleast one checkbox has to be checked before submitting form.(using javascript).
Following is the sample code.
<table border=0 cellpadding=0 cellspacing=5 width="" id="marketStationTable">

<tr>
<td align="left">
<h3>Market</h3>
</td>
<td colspan="4" align="center">
<h3>Stations</h3>
</td>
</tr>







<TR>

<td><b><input type="hidden" name="marketList0" id="marketList0" value="000000061">MADISON WI:</b></td>

<td align="right" nowrap><input type="checkbox" name="stationID"
nowrap id="stationID0" value="000000044-O-WJJO"> WJJO

</td>

<td align="right" nowrap><input type="checkbox" name="stationID"
nowrap id="stationID1" value="000000043-O-WMAD"> WMAD

</td>

<td align="right" nowrap><input type="checkbox" name="stationID"
nowrap id="stationID2" value="000000045-L-WZEE"> WZEE

</td>
</TR>

<TR>

<td><b><input type="hidden" name="marketList1" id="marketList1" value="000000063">SOUTH BEND IN:
</b></td>

<td align="right" nowrap><input type="checkbox" name="stationID"
nowrap id="stationID3" value="000000051-N-WNDU"> WNDU

</td>

<td align="right" nowrap><input type="checkbox" name="stationID"
nowrap id="stationID4" value="000000052-N-WSBT"> WSBT
</td>

<td align="right" nowrap><input type="checkbox" name="stationID"
nowrap id="stationID5" value="000000053-N-WSJV"> WSJV
</td>
</TR>

</table>

Any suggestions or help would be appreciated.
Thank you in advance
Priya
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well you can loop through the form array

document.forms[0].elements and count the check boxes that way.

or you can get all of the table rows
var trs = document.getElementsByTagName("tr");

loop through them
and get the checkboxes
var ins = trs[i].getElementsByTagName("input");

and loop through those.

Eric
 
priya suggala
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eric,
Thanks for your help and also for quick response.Its working.Below is the code I tried:

var table=document.getElementById("marketStationTable");
var trs = table.getElementsByTagName("TR");
for(var i=1;i<trs.length;i++){
var ins = trs[i].getElementsByTagName("input");
for(var j=1;j<ins.length;j++){
if((ins[j].type=='checkbox') && (!ins[j].checked)){
if(ins.length > 1){
alert("please check atleast one checkox from each row");

}
}
}
}
I have one more question:
IF I have 2 rows and 4 columns in each row .When I submit the form with out checking atleast one checkbox.
alert message is coming 8 times.Is it possible to display alert message only 2 times because only two rows are there..


can you please help me...

Thanks in advance
Priya
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You probably want to look into the break label statement in JavaScript:
http://www.devguru.com/Technologies/ecmascript/quickref/break.html

Eric
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic