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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.