Making Sure a Visitor Has Filled Out a Form Element

Making sure a visitor has supplied all the mandatory information in an HTML form is the most basic type of form validation. If you want to require your visitors to provide a name and age, you need a JavaScript function to make sure they've entered this information before the form goes to the server-side program.

Chapter 7 covers almost everything you need to know to do this. If you're feeling unsure about how JavaScript and forms work together, review Chapter 7 before reading on. If you feel confident, look at Figure 11-2, which checks whether a user has filled out the mandatory form elements, and see whether you can figure out the code before reading the line-by-line analysis.

Figure 11-2. Making ...

Get The Book of JavaScript, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.