Click to See Complete Forum and Search --> : How to check if one from a multiple list of checkboxes have been selected


justmehere
03-24-2006, 10:54 AM
i've written a cgi script which displays a dynamic list of search results and each search result item has a checkbox (this is like a shopping cart). what i need to do is check if any of these checkbox has been selected when the user selects the update cart button. since I don't know how many checkbox there will be in the list of search results (therefore checkboxes) how will i be able to check if any of the checkbox have been selected ?

many thanks.

James Gatka
03-24-2006, 11:12 AM
<html>
<head>
<script type="text/javascript">

function verifyBoxes(nBox,nMin,nMax,nDesc){

var nCount = 0;
var nSet = document.getElementsByName(nBox);
for (i=0; i<nSet.length; i++)
{if (nSet[i].checked){nCount++}}
if (nCount < nMin)
{alert('A minimun of '+nMin+ ' is required\nfor '+nDesc);return false}
if (nCount > nMax)
{alert('A maximun of '+nMax+ ' is allowed\nfor '+nDesc);return false}
return true;
}

function validate(nForm){

if (!verifyBoxes('set1',1,4,'Magazines')){return false}
if (!verifyBoxes('set2',1,1,'Books')){return false}
alert('Thank you for your submission');
}

</script>
</head>
<body>
<form onsubmit="return validate(this)">
Magazines:<br>
A: <input type='checkbox' name='set1'><br>
B: <input type='checkbox' name='set1'><br>
C: <input type='checkbox' name='set1'><br>
D: <input type='checkbox' name='set1'><br>
E: <input type='checkbox' name='set1'><br>
F: <input type='checkbox' name='set1'><br>
G: <input type='checkbox' name='set1'><br>
H: <input type='checkbox' name='set1'><br>
I: <input type='checkbox' name='set1'><br>
J: <input type='checkbox' name='set1'><br>
<br><br>
Books:<br>
K: <input type='checkbox' name='set2'><br>
L: <input type='checkbox' name='set2'><br>
M: <input type='checkbox' name='set2'><br>
N: <input type='checkbox' name='set2'><br>
O: <input type='checkbox' name='set2'><br>
P: <input type='checkbox' name='set2'><br>
Q: <input type='checkbox' name='set2'><br>
R: <input type='checkbox' name='set2'><br>
S: <input type='checkbox' name='set2'><br>
T: <input type='checkbox' name='set2'><br>
<br>
<input type='submit' name='submit' value='Submit'>
</form>
</body>
</html>

konithomimo
03-24-2006, 11:27 AM
function checkAll()
{
var ins = document.getElementsByTagName('input');
var i;
for(i=0;i<ins.length;i++)
{
if((ins[i].type=='checkbox')&&(ins[i].checked))
return true
}
return false
}

That will return true if at least one checkbox has been checked. If not then it returns false.

justmehere
03-24-2006, 01:02 PM
thanks very much..got it to work.