Validating when the user submits the form
The team at have made a video based on this article as a part of their Java Script training lessons which you can view below. Would be great if you could post full examples of code including form and not just pieces that fit into the greater framework of code.
It explains some of the code examples above as well as introducing more advanced concepts: We would be remiss in not pointing out that your Java Script and HTML code should be kept separated as far as possible. I've coded a working form, all checks are great but for some reason the hardest part is trying to stop form submission before validation.
Tried setting it up on onsubmit on both form and button and no go.
This is done using HTML5 form validation techniques and CSS, but it only validates the input format (pattern) and not the actual values.
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.
If it has not been submitted, skip the validation and display a blank form.
However, in the example above, all input fields are optional.