The value of a text input box (or a textarea or password input) is available using the syntax that tells you which option has been selected.
The illustration below shows this relationship: Note that the 'I' in selected Index needs to be capitalised - Java Script functions and variables are always case-sensitive. Read more about the humble checkbox in our HTML5 Checkbox Validation article.
For an alternative approach to client-side form validation, without Java Script, check out our new article on HTML5 Form Validation which is available now in most modern browsers. The first test in the example is therefore only necessary in order to provide a different error message when the input is blank.
In more complicated forms you will want to set conditions on the form that combine multiple elements.
When the form is submitted - either by hitting Enter or clicking on the Submit button - the to abort (cancel) the form submission. In a real-life situation you will most likely have more fields to check, and more complicated conditions, but the principle remains the same.
All you need to do is extend the command, in which case the form will be submitted.
If you're working with arrays of checkboxes to submit data to a server-side script then you might already have some grey hairs from trying to figure out how to validate the input using Java Script.
The problem is that, to have the data submitted in a 'nice' format to the server, the function returns an array containing all the selected checkbox values. Again, we can make the code a lot simpler in supporting browsers: suffix tells a server side script such as PHP to accept multiple values with that name into an array.Validating form input with Java Script is easy to do and can save a lot of unnecessary calls to the server.