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


Form validation with Javascript

Hi all, I have a checkbox html form and I need a message to appear if I checked more than 2 options. How can I do it? Need help :) Thanks

    <input type="checkbox" name="class" value="lux" checked="checked"> 1
    <input type="checkbox" name="class" value="sleep" > 2
    <input type="checkbox" name="class" value="coupe" > 3
    <input type="checkbox" name="class" value="platz" > 4
    <input type="checkbox" name="class" value="general"> 5


1 Answer

Christopher Denny
Christopher Denny
10,460 Points

I would suggest making it so that when a checkbox is click, it adds a class "active" or "checked" then write some Javascript so that if there are more than one items with the "active" class, it does what you're wanting. Heres an article for adding and removing classes http://jaketrent.com/post/addremove-classes-raw-javascript/

You can set the "onclick" event with HTML or Javascript http://www.w3schools.com/jsref/event_onclick.asp

To see if there are multiple of a class, you can check if a get Class

var checked = $('checked')
var checked = document.getElementsByClassName('checked')

has more than one item by testing if the get has more than one item in the array.


This will return true or false if there is a second item.

I hope this helps!

Thanks a lot, I will try that