Click to See Complete Forum and Search --> : Javascript Checkbox Validation Require


neriah_d
08-22-2005, 02:36 PM
I currently have 6 options in which a user MUST select 3 choices... no more... no less... I currently have a script in place that doesn't allow you to check more than 3 however you can not check anything and it goes through which is NOT what needs to happen.

Here is my current JS:



<Script Language=JavaScript>

var currCount = 0;
var maxCount = 3;

function CountChecks(isBox){

if (isBox.checked){currCount++}
else {currCount--}
if (currCount > maxCount)
{
alert('You may only select ' + maxCount + ' magazines for this package.');
currCount--;
isBox.checked = false;
}

}



</Script>



Jonny Lang (on this site) gave me the 'how-to' on my initial script... I hope you're out there and can help me add this portion to it... ;) ~hint hint~ :D

How may I add that there must be 3 checked or else it won't go on???

I can't call the individual names of the checkboxes in this script otherwise it won't work because the names are referring to specific ID #'s... I have to have this function run through as an add on to my current function or as a separate function that does not call upon the checkboxes individual names.

yearbass
08-23-2005, 12:54 AM
I have a simple example of validating checkbox that related to what you need.
This example use individual names of checkboxes.

Here is the example:

<html>
<head>
<title>Simple example of checkbox</title>
<script language="javascript">
var currCount = 0;
var maxCount = 3;
function check (i)
{
if (document.myForm.chChoose[i].checked == true)
{
if (currCount >= maxCount)
{
alert("You may only select 3 options");
document.myForm.chChoose[i].checked = false;
}
else
currCount++;
}
else
{
currCount--;
}
}
</script>
</head>

<body>

<form name="myForm">
<input type="checkbox" name="chChoose" value="Choose 1" onclick="check(0)">Choose 1
<input type="checkbox" name="chChoose" value="Choose 2" onclick="check(1)">Choose 2
<input type="checkbox" name="chChoose" value="Choose 3" onclick="check(2)">Choose 3
<input type="checkbox" name="chChoose" value="Choose 4" onclick="check(3)">Choose 4
<input type="checkbox" name="chChoose" value="Choose 5" onclick="check(4)">Choose 5
<input type="checkbox" name="chChoose" value="Choose 6" onclick="check(5)">Choose 6
</form>

</body>
</html>

Maybe it not the better way, but it is the simplest (I thought).

neriah_d
08-23-2005, 08:27 AM
yeah I can't get any JS to work with the names of my checkboxes because they are like this:

<input type="checkbox" name="id[8]" value="3" onclick="CountChecks(this)">
<input type="checkbox" name="id[9]" value="4" onclick="CountChecks(this)">
<input type="checkbox" name="id[10]" value="5" onclick="CountChecks(this)">
<input type="checkbox" name="id[11]" value="6" onclick="CountChecks(this)">
<input type="checkbox" name="id[12]" value="7" onclick="CountChecks(this)">
<input type="checkbox" name="id[13]" value="8" onclick="CountChecks(this)">


I am using OSCommerce within my site and this specific product I had to alter code for and create a separate page using the existing names of the form elements for it to work. Anytime I have tried to call upon any of these checkbox names using JS, it's like the script isn't even there so names will not work for me unless you have some other suggestion that I haven't tried...