/    Sign up×
Community /Pin to ProfileBookmark

How to check if one from a multiple list of checkboxes have been selected

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.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@James_GatkaMar 24.2006 — [CODE]<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 requirednfor '+nDesc);return false}
if (nCount > nMax)
{alert('A maximun of '+nMax+ ' is allowednfor '+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>[/CODE]
Copy linkTweet thisAlerts:
@konithomimoMar 24.2006 — function checkAll()
{
var ins = document.getElementsByTagName('input');
var i;
for(i=0;i&lt;ins.length;i++)
{
if((ins[i].type=='checkbox')&amp;&amp;(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.
Copy linkTweet thisAlerts:
@justmehereauthorMar 24.2006 — thanks very much..got it to work.
×

Success!

Help @justmehere spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...