I have a multiple select box with which i need to validate w/ javascript upon form submission. But then i need to use it as an array with php to enter into the db. the JS isn't working when the select box is named with brackets (i.e. selZC[]). Can someone tell me how o fix the below code. thanks!
<script language = "JavaScript">
<!--
function CheckNum(num_sel, res_zip) {
// initialize the counter
var selZC = document.forms[0].elements['ZipCodes[]'];
var isZipSelected = 0;
var Counter = 0;
// Loop through the select box to see how many are selected;
for (i=0; i<document.MyForm.selZC .length; i++){
// If an element is selected, increment the counter
if (document.MyForm.selZC .selected == true)
{
if (document.MyForm.selZC .value == res_zip)
{
isZipSelected = 1;
}
Counter++;
}
}
// If the counter is greater than num_zip, display an alert message.
if (Counter > num_sel){
if (confirm("You are allowed up to " + num_sel + " zip codes and you have selected " + Counter + " zip codes.")){
return true;
}else{
return false;
}
} else {
if (isZipSelected == 0)
{
if (confirm("Are you sure you want to do this?")){
document.forms[0].action='targetdiner.php?u=y';
return true;
}else{
return false;
}
return false;
}
else
{
document.forms[0].action='targetdiner.php?u=y';
return true;
}
}
}
//-->
</script>
<? echo $ZipCodes[0]; ?>
<SELECT SIZE=15 MULTIPLE name="ZipCodes[]">