Validating checkbox using javascript

Rated 3.80/5 based on 515 customer reviews

See also Jeff Howden's excellent article Forms & Java Script Living Together in Harmony for some of the most common usability errors and their solutions.

First of all, you should have a clear idea of what happens when a user submits a form that has a Java Script validation script: : by the Java Script and by the CGI script.

Hi I am trying to validate my webpage which has 2 checkboxes on it.

I only want the user to select 1 checkbox and if they do select 2 and click submit then an alert box will appear telling them that they have selected 2 boxes.

The advantage for the user is that it's obvious whick element is causing the problem and there's no alert window that needs to be clicked away.

You can set the value to be a ‘1’ or ‘on’ instead of ‘Yes’.

Some, but not all, browsers will recognise and enforce this: The advantage of the HTML5 form validation is that it happens before our Java Script is called, displays instructions and points the user to the relevant element.

Here you can see screen captures from Firefox and Chrome: Text alert messages are generated entirely by the browser and will even translate automatically into different languages - something that would be almost impossible using just Java Script.

How do I stop the page from getting submitted if an alert box appears? For example, imagine that you had a questionnaire that asked the user a number of questions in radiobutton and checkbox form.Imagine that you had a series of checkboxes that corresponded to the hobbies sports, reading, and swimming, and you asked the ready to check all hobbies that he enjoyed.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.

Leave a Reply