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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Göran AnderssonCommented:
You forgot these:

count = 0;
str = '';
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ussherAuthor Commented:
CHAMPION!! Thanks very much.  worked perfectly.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.