Chapter 14. Creating validation rules

 

This chapter covers

  • The jQuery Validation plugin
  • Adding validation rules

Although not part of jQuery itself, the Validation plugin from Jörn Zaefferer is a widely used plugin that has its own extension point (http://jqueryvalidation.org). The plugin helps to ensure that only valid data is sent to the server when a form is submitted, avoiding unnecessary requests that would only result in a user-correctable error, such as missing required fields or incorrectly formatted email addresses.

The built-in rules provided by the plugin include required for mandatory fields, digits or number for numeric input, min and max for minimum and maximum values, email and url for email address and URL formats, and equalTo for field comparisons. You can combine these for a single field to create more complex validations, such as a mandatory numeric field with a maximum value.

The plugin manages the application of these rules at the appropriate times—for instance, when field entry occurs or when the form is submitted—and displays suitable error messages if a field is invalid. The aim is to provide an unobtrusive user experience: to display error messages only after some user action, and to remove them as soon as possible. Placement of error messages and their appearance can be controlled by the user if necessary, although the default behavior is often appropriate.

14.1. The Validation plugin

14.2. Adding a validation rule

14.3. Adding a multiple-field validation rule

14.4. Summary

sitemap