Javascript Checkbox Validation with <form> and elements

im trying to get a javascript function i got from somewhere on the net to work with my system.

I only wan the form to submit if there is at least one of the checkboxes checked.  If nothing is checked then pop up an allert

I think the reason for it not working is that there are variable element ids that change for every page.
products_id[(this number is dynamic)]
<form name="multiple_products_cart_quantity" Method="post" onsubmit="return confirmSubmit();">
<input value="1" name="products_id[2973]" type="checkbox">
<input value="1" name="products_id[2975]" type="checkbox">
<input value="1" name="products_id[2978]" type="checkbox">
<input value="1" name="products_id[2980]" type="checkbox">
<input value="1" name="products_id[2981]" type="checkbox">
<input value="1" name="products_id[2982]" type="checkbox">
<input src="button_in_cart.gif" alt="Add to Cart" title=" Add to Cart " type="image"> 
</form>
 
 
function confirmSubmit(){
count = 0;
str = '';
    for(x=0; x<document.multiple_products_cart_quantity.elements["products_id[]"].length; x++){
        if(document.multiple_products_cart_quantity.elements["products_id[]"][x].checked==true){
            str += document.multiple_products_cart_quantity.elements["products_id[]"][x].value + ',';
            count++;
        }
    }
    
    if(count==0){
        alert("You must choose at least 1");
        return false;
    }
    return true;
}

Open in new window

LVL 1
ussherAsked:
Who is Participating?
 
Göran AnderssonConnect With a Mentor Commented:
You forgot these:

count = 0;
str = '';
0
 
Göran AnderssonCommented:
Loop through all the elements and use a regular expression to find the elements that match the pattern:
var elm = document.multiple_products_cart_quantity.elements;
for (i=0; i<elm.length; i++) {
	if (/^products_id\[\d+\]$/.test(elm[i].name) {
		if (elm[i].checked) {
			str += elm[i].value + ',';
			count++;
		}
	}
}

Open in new window

0
 
ussherAuthor Commented:
Thanks for the help GreenGhost.
firebug told me to add another ) to the end of the 'if' line which i tried, but it still doesnt seam to be working.

i changed all the returns to false but it still submits.  
function confirmSubmit(){
var elm = document.multiple_products_cart_quantity.elements;
for (i=0; i<elm.length; i++) {
        if (/^products_id\[\d+\]$/.test(elm[i].name)) {
                if (elm[i].checked) {
                        str += elm[i].value + ',';
                        count++;
                }
        }
}
    if(count==0){
        alert("You must choose at least 1");
        return false;
    }
    else if(count>3){
        alert("You can choose a maximum of 3");
        return false;
    }
    else {
        alert("the else for other options");
        return false;
    }
        alert("something else");
        return false;
        
}

Open in new window

0
 
ussherAuthor Commented:
CHAMPION!! Thanks very much.  worked perfectly.
0
All Courses

From novice to tech pro — start learning today.