I got 7 checkboxes which are basically sizes for clothing, XS, S, M, L, XL, XXL, and 3XL and I am trying to validate them using javascript, all I need is for it to know if any have been checked, and if none haven't bring up the warning message I already have in place. The only problem I've been getting so far is accessing the check button group I've made, which is an array called Sizes[].
Here's the form code:
- Code: Select all
<?php
$CBSizes = array("XS","Small", "Medium", "Large", "XL", "XXL", "3XL");
?>
<span id="mySpan7" class="errmsg"><?php echo $Message_ASizes ?></span><br>
<span class="head4">Available Sizes:</span><br>
<table id="Sizes"><tr>
<?php
$X=0;
while($X!=7)
{
$Size = $CBSizes[$X];
echo"<td>$Size:</td>";
echo"<td><input type='checkbox' name='Sizes[]' value='$Size'";
if($SizeA[$Size]==true){echo"Checked";}?>></td></tr><tr>
<?php
$X++;
}
?>
</tr></table>
As you can see I use the array CBSizes to grab the names per value of $X, and place all the Sizes into the Sizes[] array. I have a method already in
PHP for validating it but I haven't got a working one for Javascript.
Here's the code that's in my main validation function for that section:
- Code: Select all
numBoxesSelected = 0;
var valuesSelected = new Array;
for (var i=1; i <= numBoxes; i++)
{
if (eval("document.AddProduct.Sizes" + i + ".checked"))
{
valuesSelected[numBoxesSelected] = eval("document.AddProduct.Sizes" + i + ".value");
numBoxesSelected++;
}
}
if(numBoxesSelected ==0)
{
document.getElementById('mySpan7').innerHTML='! Please choose any relevant Sizes!';
}
else
{
document.getElementById('mySpan7').innerHTML='';
}