| Author |
How to validate that in each row how many checkboxes are checked
|
priya suggala
Greenhorn
Joined: May 17, 2006
Posts: 2
|
|
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
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15357
|
|
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
Joined: May 17, 2006
Posts: 2
|
|
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
Rancher
Joined: Nov 08, 2001
Posts: 15357
|
|
You probably want to look into the break label statement in JavaScript: http://www.devguru.com/Technologies/ecmascript/quickref/break.html Eric
|
 |
 |
|
|
subject: How to validate that in each row how many checkboxes are checked
|
|
|