skip to content »

Validating multiple checkboxes using javascript

validating multiple checkboxes using javascript-53

The problem may be in the custom validator but I can not spot any error and it is a really simple piece of code. Until now I treated this case (many checkboxes representing a unique group) as if they were radiobuttons, that's why I implemented it that way.

Make sure the check in the PHP code is also updated accordingly.Please note that the checkboxes have the exact same name ( form Door[ ] ). Using the same name indicates that these checkboxes are all related.Using [ ] indicates that the selected values will be accessed by PHP script as an array.It is checking that at least one checkbox is checked.I only don't like that I have to manually call validate for one Checked-Validator on form submission. In the case of a checkbox multiple values can be selected so validation is done if none of the checkboxes are ticked.

You can also validate by requiring the user to select a minimum of one value or two value according to your requirement.

I've implemented a custom validator to be able to verify that at least one of many checkboxes is selected.

However, sometimes when checking and un-checking the checkboxes, the submit button still appears as disabled and some other times, the checkboxes are still marked as with error (in red).

In Javascript we create a function which checks if some value is entered in the textbox or selected in the case of combobox, radiobutton, checkbox.

If a textbox is empty or an option is not selected we can use javascript to display an alert message, and highlight the empty field and also we should make sure the page stays the same and doesn’t get “submitted” Javascript validation is a client side validation and relies on the web browser so it should not be solely relied upon Textbox is the simplest element to validate if empty or not.

We need to check if it is equal to null or “” because different browsers act differently so its better to check for both the conditions.