Validating checkbox in javascript

There are times when we need to add javascript validation to check if at least one of a group of checkbox options has been checked.

Here is a simple function that’ll solve the problem and works in most of the browsers.

It's so easy to make subtle, impossible to spot mistakes with your regular expression.

If you can't use expression forces Java Script to do type coercion on your input value; it must first be interpreted as a number for the subtraction operation.

This entry was posted on January 22, 2010, pm and is filed under Javascript.

Since the left hand side is now numeric, type coercion is again used.If not, an alert is displayed and focus is moved to the checkbox.Not the prettiest solution, but functional in all browsers with Java Script enabled.Adding HTML5 validation to the checkbox is actually very simple.All you need to do is include a This tells the browser that the form should not be allowed to submit without the checkbox checked.

Leave a Reply