View Full Version : Counting Radio Buttons
GregLuce
05-01-2003, 03:20 PM
Is there a way to validate that say 10 of a list of over 100 radio buttons are selected?
Here's an example of the form:
<td nowrap><input type="radio" name="option_220056" value="1">
<input type="radio" name="option_220056" value="2">
<input type="radio" name="option_220056" value="3"></td>
<td nowrap><input type="radio" name="option_219632" value="1">
<input type="radio" name="option_219632" value="2">
<input type="radio" name="option_219632" value="3"></td>
I have a long list of dynamically generated records where the user needs to select no more than 10 "1"s. I'm sure there must be a way to count them onclick or something. Help! :-)
requestcode
05-01-2003, 03:41 PM
Do you want to check as the click on the radio buttons or when the go to submit the form? Also is it only the ones with a value of "1" that you want to check?
GregLuce
05-01-2003, 04:07 PM
I think I want to count the number of buttons selected "1" when it's clicked, I figure if I can get the "1"s working I can figure out the 2s and 3s.
cheesebagpipe
05-01-2003, 05:29 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" />
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">
var rad_limit = new Object(); //rad_limit[radio value] = maximum;
rad_limit['1'] = 5;
rad_limit['2'] = 4;
rad_limit['3'] = 7;
function limit_by_value(oLastRad) {
var el, i = 0, iChecked = 0, oForm = oLastRad.form;
while (el = oForm.elements[i++])
if (el.type == 'radio' && el.checked && el.value == oLastRad.value)
++iChecked;
if (iChecked > rad_limit[oLastRad.value]) {
var sMsg = '\nYou have exceeded the maximum number of these items allowed.\n\n';
sMsg += 'Please select no more than ' + rad_limit[oLastRad.value] + '.\n\nThanks.\n\n';
alert(sMsg);
return (oLastRad.checked = false);
}
return true;
}
</script>
</head>
<body>
<h5 style="font-family:monospace;">MAXIMUM<br />BOXES&nbsp;&nbsp;&nbsp;
item 1--->[5] item 2--->[4] item 3--->[7]<br />ALLOWED</h5>
<form>
<table cellspacing="12" border="1" style="color:white;background:olive;margin-left:120px;">
<tr>
<td>
<input type="radio" name="r1" value="1" onclick="limit_by_value(this)" /> item 1<br />
<input type="radio" name="r1" value="2" onclick="limit_by_value(this)" /> item 2<br />
<input type="radio" name="r1" value="3" onclick="limit_by_value(this)" /> item 3<br />
</td><td>
<input type="radio" name="r2" value="1" onclick="limit_by_value(this)" /> item 1<br />
<input type="radio" name="r2" value="2" onclick="limit_by_value(this)" /> item 2<br />
<input type="radio" name="r2" value="3" onclick="limit_by_value(this)" /> item 3<br />
</td><td>
<input type="radio" name="r3" value="1" onclick="limit_by_value(this)" /> item 1<br />
<input type="radio" name="r3" value="2" onclick="limit_by_value(this)" /> item 2<br />
<input type="radio" name="r3" value="3" onclick="limit_by_value(this)" /> item 3<br />
</td></tr><tr><td>
<input type="radio" name="r4" value="1" onclick="limit_by_value(this)" /> item 1<br />
<input type="radio" name="r4" value="2" onclick="limit_by_value(this)" /> item 2<br />
<input type="radio" name="r4" value="3" onclick="limit_by_value(this)" /> item 3<br />
</td><td>
<input type="radio" name="r5" value="1" onclick="limit_by_value(this)" /> item 1<br />
<input type="radio" name="r5" value="2" onclick="limit_by_value(this)" /> item 2<br />
<input type="radio" name="r5" value="3" onclick="limit_by_value(this)" /> item 3<br />
</td><td>
<input type="radio" name="r6" value="1" onclick="limit_by_value(this)" /> item 1<br />
<input type="radio" name="r6" value="2" onclick="limit_by_value(this)" /> item 2<br />
<input type="radio" name="r6" value="3" onclick="limit_by_value(this)" /> item 3<br />
</td></tr><tr><td>
<input type="radio" name="r7" value="1" onclick="limit_by_value(this)" /> item 1<br />
<input type="radio" name="r7" value="2" onclick="limit_by_value(this)" /> item 2<br />
<input type="radio" name="r7" value="3" onclick="limit_by_value(this)" /> item 3<br />
</td><td>
<input type="radio" name="r8" value="1" onclick="limit_by_value(this)" /> item 1<br />
<input type="radio" name="r8" value="2" onclick="limit_by_value(this)" /> item 2<br />
<input type="radio" name="r8" value="3" onclick="limit_by_value(this)" /> item 3<br />
</td><td>
<input type="radio" name="r9" value="1" onclick="limit_by_value(this)" /> item 1<br />
<input type="radio" name="r9" value="2" onclick="limit_by_value(this)" /> item 2<br />
<input type="radio" name="r9" value="3" onclick="limit_by_value(this)" /> item 3
</td></tr><tr><td colspan="3" align="center">
<br /><input type="reset" />
</td></tr></table>
</form>
</body>
</html>
GregLuce
05-01-2003, 07:15 PM
I'm sorry, I can't understand this script. I need to count how many 1s are checked when each "1" value radio button is clicked. What is the function I need to call in the onclick event?
cheesebagpipe
05-01-2003, 07:31 PM
Eesh...you did say 'radios' didn't you. Oh, well, I was up late last night. :confused: Re-did the above. Here's how you set the limit for each radio value:
rad_limit['1'] = 5;
The number in red is the value, the one in blue is the maximum allowed. Let me know asap if there's something you don't follow.
GregLuce
05-01-2003, 08:50 PM
Very good! Although it takes about 5 seconds to do it's thing. Do you think if we made 3 different functions for the three different values it would be faster? If when you check a 1 it would only have to loop through the other 1s to get a total instead of looping through all the radio buttons? I have about 300 records on the form.
cheesebagpipe
05-02-2003, 03:46 AM
I get carpal tunnel just thinking about your form. :eek: Anyway, came up with a more efficient way. Seems to work:
radLimit.max['1'] = 5;
[holds max number for each value, set as described above]
radLimit.n['1'] = 0;
[holds current number for each value, set start value for each, equaling zero]
[if any are default checked, increase value to reflect that]
is_checked
[property of each array of (3) buttons, holds which one is currently checked, so we can decrement it if deselected]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" />
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">
radLimit.max= new Object(); //radLimit.max[radio value] = maximum;
radLimit.max['1'] = 5;
radLimit.max['2'] = 4;
radLimit.max['3'] = 7;
radLimit.n = new Object(); //set ---> radLimit.n[radio value] = 0; <--- for each of the above
radLimit.n['1'] = 0;
radLimit.n['2'] = 0;
radLimit.n['3'] = 0;
function radLimit(oRadio) {
var oForm = oRadio.form, grp = oForm.elements[oRadio.name]; //get array
if (typeof grp.is_checked != 'undefined' && grp.is_checked == oRadio) return; // already checked
++radLimit.n[oRadio.value]; //bump up number checked
if (radLimit.n[oRadio.value] > radLimit.max[oRadio.value]) { //too many?
var sMsg = '\nYou have exceeded the maximum number of these items allowed.\n\n';
sMsg += 'Please select no more than ' + radLimit.max[oRadio.value] + '.\n\nThanks.\n';
alert(sMsg);
--radLimit.n[oRadio.value]; //decrement number checked
if (typeof grp.is_checked != 'undefined') grp.is_checked.checked = true; //re-check previous
return false;
} else {
if (typeof grp.is_checked != 'undefined') --radLimit.n[grp.is_checked.value]; //unchecking, decrement counter
grp.is_checked = oRadio; //for next time, this one currently checked
return true;
}
}
</script>
</head>
<body>
<div align="center">
<h5 style="font-family:monospace;border:1px olive solid;padding:5px;background:ivory;">MAXIMUM RADIOS ALLOWED
<br /><br />item 1 ---> (5) item 2 ---> (4) item 3 ---> (7)</h5>
<form>
<table cellspacing="12" border="1" style="color:white;background:olive">
<tr>
<td>
<input type="radio" name="r1" value="1" onclick="return radLimit(this)" /> item 1<br />
<input type="radio" name="r1" value="2" onclick="return radLimit(this)" /> item 2<br />
<input type="radio" name="r1" value="3" onclick="return radLimit(this)" /> item 3<br />
</td><td>
<input type="radio" name="r2" value="1" onclick="return radLimit(this)" /> item 1<br />
<input type="radio" name="r2" value="2" onclick="return radLimit(this)" /> item 2<br />
<input type="radio" name="r2" value="3" onclick="return radLimit(this)" /> item 3<br />
</td><td>
<input type="radio" name="r3" value="1" onclick="return radLimit(this)" /> item 1<br />
<input type="radio" name="r3" value="2" onclick="return radLimit(this)" /> item 2<br />
<input type="radio" name="r3" value="3" onclick="return radLimit(this)" /> item 3<br />
</td></tr><tr><td>
<input type="radio" name="r4" value="1" onclick="return radLimit(this)" /> item 1<br />
<input type="radio" name="r4" value="2" onclick="return radLimit(this)" /> item 2<br />
<input type="radio" name="r4" value="3" onclick="return radLimit(this)" /> item 3<br />
</td><td>
<input type="radio" name="r5" value="1" onclick="return radLimit(this)" /> item 1<br />
<input type="radio" name="r5" value="2" onclick="return radLimit(this)" /> item 2<br />
<input type="radio" name="r5" value="3" onclick="return radLimit(this)" /> item 3<br />
</td><td>
<input type="radio" name="r6" value="1" onclick="return radLimit(this)" /> item 1<br />
<input type="radio" name="r6" value="2" onclick="return radLimit(this)" /> item 2<br />
<input type="radio" name="r6" value="3" onclick="return radLimit(this)" /> item 3<br />
</td></tr><tr><td>
<input type="radio" name="r7" value="1" onclick="return radLimit(this)" /> item 1<br />
<input type="radio" name="r7" value="2" onclick="return radLimit(this)" /> item 2<br />
<input type="radio" name="r7" value="3" onclick="return radLimit(this)" /> item 3<br />
</td><td>
<input type="radio" name="r8" value="1" onclick="return radLimit(this)" /> item 1<br />
<input type="radio" name="r8" value="2" onclick="return radLimit(this)" /> item 2<br />
<input type="radio" name="r8" value="3" onclick="return radLimit(this)" /> item 3<br />
</td><td>
<input type="radio" name="r9" value="1" onclick="return radLimit(this)" /> item 1<br />
<input type="radio" name="r9" value="2" onclick="return radLimit(this)" /> item 2<br />
<input type="radio" name="r9" value="3" onclick="return radLimit(this)" /> item 3
</td></tr><tr><td colspan="3" align="center">
<br /><input type="reset" />
</td></tr></table>
</form>
</div>
</body>
</html>
GregLuce
05-02-2003, 04:04 AM
That works sooo perfect thanks! Let me know where I can send you a starbucks gift certificate. I need to buy you a venti mocha.
cheesebagpipe
05-02-2003, 04:36 AM
I'll settle for a margarita. :D Let me know when something doesn't compute.
btw: you might want to remove all the onclicks from your inline HTML and assign them as so:
<input type="radio" name="r9" value="3" /> item 3
</td></tr><tr><td colspan="3" align="center">
<br /><input type="reset" />
</td></tr></table>
</form>
<script type="text/javascript" language="javascript">
//register handlers
var el, i = 0, oForm = document.forms[0];
while (el = oForm.elements[i++])
if (el.type == 'radio') el.onclick = function() { return radLimit(this) };
</script>
</div>
That's assuming they're the only radios on the page, in the only form. Saves a lot of page weight. If you use this, be sure and test by trying to click on the lowest radio on the form as soon as you can (during loading) . JS usually wins the race, but, can never be too sure. cya
GregLuce
05-02-2003, 01:24 PM
So I don't need the onclick on the radio buttons? I'll try that, but how will the function get called? I like it counting every time you click a radio button.
cheesebagpipe
05-02-2003, 04:22 PM
Hey Greg...
---------------------------------------------------------------------------------------------------------------
<input type="radio" name="r1" value="1" onclick="return radLimit(this)" /> item 1<br />
---------------------------------------------------------------------------------------------------------------
...is the same as:
-------------------------------------------------------------------------
<input type="radio" name="r1" value="1" /> item 1<br />
<script type="text/javascript" language="javascript">
var el = document.forms[0].elements['r1'];
el.onclick = function() { return radLimit(this) };
</script>
-------------------------------------------------------------------------
One onclick handler is assigned in HTML, the other with JavaScript. Same result.
GregLuce
05-08-2003, 09:39 PM
Hey Cheese. How about if my input names are dynamically generated? The inputs you're doing there are named "r1". Mine are "OPTION_124356", "OPTION_43562"...
cheesebagpipe
05-09-2003, 05:46 PM
Greg...had a bear of a time tracking down a bug in this one...think I finally got it (radios are odd). Try this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" />
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">
maxrad.max= new Object(); //maxrad.max[radio value] = maximum;
maxrad.max['1'] = 2;
maxrad.max['2'] = 3;
maxrad.max['3'] = 4;
maxrad.checked = new Object(); //set ---> maxrad.checked[radio value] = 0; <--- for each of the above
maxrad.checked['1'] = 0;
maxrad.checked['2'] = 0;
maxrad.checked['3'] = 0;
function maxrad(oRadio) {
if (typeof maxrad[oRadio.name] != 'undefined' && maxrad[oRadio.name] == oRadio) return false;
else {
if (maxrad.checked[oRadio.value] == maxrad.max[oRadio.value]) {
var sMsg = '';
sMsg += '\n_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|';
sMsg += '_|_|_|_|_|_|_|_|_| ';
sMsg += '\n\nYou have exceeded the maximum number of these items allowed.\n';
sMsg += '\tPlease select no more than ' + maxrad.max[oRadio.value] + ' of them.';
sMsg += '\n\t\tThank you.\n';
sMsg += '\n_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|';
sMsg += '_|_|_|_|_|_|_|_|_|';
alert(sMsg);
oRadio.checked = false;
} else {
++maxrad.checked[oRadio.value];
if (typeof maxrad[oRadio.name] != 'undefined') --maxrad.checked[maxrad[oRadio.name].value];
maxrad[oRadio.name] = oRadio;
}
}
}
</script>
</head>
<body bgcolor="#ddbb88">
<div align="center">
<h5 style="width:400px;font-family:monospace;border:1px black solid;padding:5px;background:ivory;">MAXIMUM RADIOS ALLOWED
<br /><br />item 1 ---> (2) item 2 ---> (3) item 3 ---> (4)</h5><br /><br />
<form>
<table cellspacing="12" cellpadding="4" border="1"
style="color:white;background:tan;border:6px silver groove;">
<tr>
<td>
<input type="radio" name="OPTION_124356" value="1" /> item 1<br />
<input type="radio" name="OPTION_124356" value="2" /> item 2<br />
<input type="radio" name="OPTION_124356" value="3" /> item 3<br />
</td><td>
<input type="radio" name="OPTION_434562" value="1" /> item 1<br />
<input type="radio" name="OPTION_434562" value="2" /> item 2<br />
<input type="radio" name="OPTION_434562" value="3" /> item 3<br />
</td><td>
<input type="radio" name="OPTION_765343" value="1" /> item 1<br />
<input type="radio" name="OPTION_765343" value="2" /> item 2<br />
<input type="radio" name="OPTION_765343" value="3" /> item 3<br />
</td></tr><tr><td>
<input type="radio" name="OPTION_321754" value="1" /> item 1<br />
<input type="radio" name="OPTION_321754" value="2" /> item 2<br />
<input type="radio" name="OPTION_321754" value="3" /> item 3<br />
</td><td>
<input type="radio" name="OPTION_867094" value="1" /> item 1<br />
<input type="radio" name="OPTION_867094" value="2" /> item 2<br />
<input type="radio" name="OPTION_867094" value="3" /> item 3<br />
</td><td>
<input type="radio" name="OPTION_089756" value="1" /> item 1<br />
<input type="radio" name="OPTION_089756" value="2" /> item 2<br />
<input type="radio" name="OPTION_089756" value="3" /> item 3<br />
</td></tr>
</table>
</form>
</div>
<script type="text/javascript" language="javascript">
//register handlers
var el, i = 0, oForm = document.forms[0];
while (el = oForm.elements[i++])
if (el.type == 'radio') el.onclick = function() { maxrad(this) };
</script>
</body>
</html>
The radio names (as per your question) are irrelevent, part of your generated HTML. Please knock this around a bit and see if it's OK.
cheers, cbp
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.