Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML

Validating that at least one checkbox is checked.

hi, I love the site.

I was able to create a working custom form!

I am struggling with how to validate that at least one checkbox is checked in a multiple select question.

here is a sample question:

<p> 9. What language do you speak at home?<p>

<input type="checkbox" name="language" value="onlyspanish"> Only Spanish <br />
<input type="checkbox" name="language" value="mostlyspanish"> Mostly Spanish but some English <br />
<input type="checkbox" name="language" value="spanishenglish"> Spanish and English equally <br />
<input type="checkbox" name="language" value="mostlyenglish"> Mostly English but some Spanish <br />

Any help would be appreciated. I couldn't find any video on this one.

Thank you.

Hello, check out this video for a hint..Happy coding!

http://www.youtube.com/watch?v=blsjn0RAgpY

2 Answers

Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

Hi Julie Diaz-Asper,

How are you doing your form validation? Is it with JavaScript, or are you using a sever side language like PHP?

Hi..

Trying to do via Javascript with a onclick on submit.

Struggling to figure out a clean way to validate a multiple question form. Especially a good way to check to see that at least one checkbox is checked for more than one question.

I found this example that looks good but struggling to find a way to do this across multiple questions.

function valthisform() {
    var chkd = document.FC.c1.checked || document.FC.c2.checked || document.FC.c3.checked || document.FC.c4.checked

    if (chkd == true) {}
    else {
        alert ("please check a checkbox")
    }
}

http://stackoverflow.com/questions/11787665/making-sure-at-least-one-checkbox-is-checked

Also be good to find away for the users data to be saved if they have an errorr. It's wiping out right now on my form..

any hints, resources or help appreciated..

thanks. Julie

Nick Pettit
Nick Pettit
Treehouse Teacher

It's much easier to do this type of thing with jQuery, rather than straight JavaScript. I recommend you check out our Build an Interactive Website course if you haven't already. Andrew Chalkley walks through forum validation within the first few stages.

You may also want to look at this thread on Stack Overflow: http://stackoverflow.com/questions/901712/check-checkbox-checked-property-using-jquery

I hope that helps! :)

very helpful.. I will check out the resources.. I had a feeling i was going in the wrong direction. Thanks.