Otherwise a browser with Java Script disabled, or a hacker trying to compromise your site, can easily by-pass client-side validation.
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.
Inside the function, the arguments (the parameters) behave as local variables. If the function was invoked from a statement, Java Script will "return" to execute the code after the invoking statement. The return value is "returned" back to the "caller": Since local variables are only recognized inside their functions, variables with the same name can be used in different functions.
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 purpose of a form validation script is to return a boolean value ( to reference form fields, but that can lead to namespace conflicts and why make things more complicated than necessary.
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.
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.
Radio buttons are implemented as if they were an array of checkboxes.