javascript at least 1 checkbox checked before form submit

Hey folks,

I have a fairly simple setup, a form with 3 checkboxes and a submit button, and I'm trying to ensure the user checks at least 1 of the checkboxes before submitting the form, but I seem to be doing something incorrectly. It doesn't produce an error, but it doesn't appear to do anything else either.

Here's what I'm using:

JAVASCRIPT:
-----------------
function validate_checkbox() {

var ckbx_arr=document.getElementsByName('item_select');

var ckbx_arr_ln=ckbx_arr.length;
for(var i=0;i<ckbx_arr_ln;i++){
if(ckbx_arr[i].checked)return true;
}
return false;

}
---------------------

HTML:
---------------------
            <form action="processform.php" method="post" name="ChooseItems">
                  3:<input type="checkbox" name="item_select" value="3">
                  2:<input type="checkbox" name="item_select" value="2">
                  1:<input type="checkbox" name="item_select" value="1">

            <input name="Submit" type="submit" value="submit" onClick="validate_checkbox()" />
            </form>



Where I'm going wrong at?

Thanks!

jmoriartyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

StealthyDevCommented:
you should give

onClick="return validate_checkbox()"

return keyword is missing
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
StealthyDevCommented:
If required you can alert the user to select atleast one check-box like this:


function validate_checkbox() {

var ckbx_arr=document.getElementsByName('item_select');

var ckbx_arr_ln=ckbx_arr.length;
for(var i=0;i<ckbx_arr_ln;i++){
if(ckbx_arr[i].checked)return true;
}
alert("please select atleast one option!");
return false;

}

Open in new window

0
RobinSoftware EngineerCommented:
Can you try changing the onClick event from

onClick="validate_checkbox()"

to

onClick="return validate_checkbox()"
0
jmoriartyAuthor Commented:
duh, always something simple you overlook, thanks guys.
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
Web Components

From novice to tech pro — start learning today.