If you're working with arrays of checkboxes to submit data to a server-side script then you might already have some grey hairs from trying to figure out how to validate the input using Java Script.
The value of a text input box (or a textarea or password input) is available using the syntax that tells you which option has been selected.
The illustration below shows this relationship: Note that the 'I' in selected Index needs to be capitalised - Java Script functions and variables are always case-sensitive. Read more about the humble checkbox in our HTML5 Checkbox Validation article.
I tested your first piece of code on a form and it does not work at all.
Tried setting it up on onsubmit on both form and button and no go.
Normally you would modify this so that you could submit or not submit the form based on the number of items selected. For example, a text input that only needs to have a value if a checkbox is checked: Using simple logical operators and the functions supplied above you can do all sorts of client-side form validation.