Using JavaScript to perform additional validation on a form is very easy to do, you simply need to add an onsubmit event set in the form that either has your JavaScript code in it or calls a JavaScript function. This event gets called when the submit button is pressed and returns true if the form is valid or false if there are issues with the form. Below is the sample form that we will be using for this section:
Within the function that you created to handle the validation (validateForm in this case) you write code to check if the form is good. This can be combined with the built-in checks that HTML provides, which you can check using JavaScript by using the checkValidity() function on the element you want to verify. If everything in the form meets your requirements then you return true which will cause the submit action to be performed, or false which cancels the submit.
You need to provide error information yourself. Changing the style of invalid form items is the current trend. This can be done by using the id of the element that is at fault, but you will notice that our sample code seems to have not specified any ids for the elements. This was done on purpose to demonstrate another feature of JavaScript, or more precisely, the DOM model.
The document object has a forms variable that holds information about all the forms on the page. This is a multi-dimensional array with the first [] being the name of the form and the second [] being the name of the input item. We will be covering arrays, multi-dimensional arrays, and associative arrays in chapter 7 but for using them with forms it is fairly straightforward. You simply get the element putting the form you want in the first square bracket and the element in the second
Typically, you would then grab the value or call a method. If you are just grabbing a value and not planning on using the element for other tasks, you can do the following:
Testing text boxes generally involves making sure correct length, or some other checks such as making sure first letter is a letter and capitalized. As numbers can be returned as string, it is a good practice to use the Math.floor function to force them into an integer.
Because radio buttons have a value that indicates which button is checked, the value is the name of the radio button that is checked for that group. An empty string is returned if no radio button is checked. Checkbox values are the name of the checkbox so to get if they are checked, use the checked attribute.
And, as mentioned already, if there are no problems with the form you should return true.
At this point we have covered most things you would need to know about forms. Now would be a good time to try to put what you learned into practice as using the concepts you have been shown is how you enforce what you have learned. The next section is this chapter’s project.
This chapter's summary cheat sheet.
How forms are traditionally used.
How to set up a form in HTML.
Getting text input from the user.
Making your input fields more specific and semantic.
Making forms look more like printed forms.
How to use Checkboxes and Radio buttons.
Using numbers and dates gives your user better GUI controls.
The reset, button, image, color, and file input types.
The select element lets users select options from a drop-down list.
The datalist elements lets you create pull-down lists for input elements.
Displaying values in a form.
Built in dialog boxes and pre-dialog tag handmade dialogs.
The <dialog> tag and using it to create dialog boxes.
Review of validating forms in HTML.
How to use JavaScript to prevent invalid forms from being submitted.
Chapter project: Trivia game
Solution for the Trivia gaame