Forms appear all over the web. However, they often lack one essential element: validation. Form validation is quite important for both web users and developers. Users are guided by receiving clearly stated responses to errors, whereas developers will find that validation keeps bad data out of the data source.
There are different kinds of form validation. Perhaps the most frequently applied type of validation is one that ensures that users fill out required fields correctly. Other validations specify which characters users can or cannot use in an entry, or ensure that users only enter data in a certain format (for example, mm/dd/yyyy for a date).
Adobe Dreamweaver introduced advanced validation using the Adobe Spry framework for Ajax in Creative Suite 3 and has enhanced its functionality for the Creative Suite 4 release. By and large, the Spry form validation objects are much more sophisticated and functional than the standard Validate Form client-side behavior, which was available in Dreamweaver since the very first release. With Spry form validation objects, you get a more robust set of checks and multi-state messaging. You can, for example, display different messages when a required field is omitted and when the incorrect data has been entered.
This article explores the general use of Spry Validation Form objects and the new form objects available in Dreamweaver CS4. You'll quickly get a grasp of just how powerful and easy to use the Spry Validation Form objects are.
To complete this tutorial you will need to install the following software and files:
Famliarity with the Dreamweaver CS4 workspace and the Spry framework for Ajax.
The form I validate in this article is a relatively simple one that includes four text fields (Name, Password, Confirm Password, and E-mail) and one check box (I agree), as shown in Figure 1. For this example, I'll start with the most basic use case and work up to more complex scenarios.

Figure 1. This is the sample form to validate.
If I had to choose one word to describe the Spry form validation implementation in Dreamweaver, it would be elegant. To add a form element to the page—complete with validation and multi-level error messages—you simply drag and drop it in your HTML page. All customizations after insertion are made through the custom Property inspector or in the Document window itself.
Follow these initial steps for adding Spry form validation objects to your page:
If you'd like to follow along, download and unpack the sample ZIP file that accompanies this tutorial and open the file form_start.htm.
This starting point already includes
a table within a <form> tag. I generally structure my forms with
tables so I can quickly get a column of right-aligned labels adjacent to my
form fields. Although you're free to use CSS to create the form structure,
tables are valid for holding form data as well.
From the Forms category of the Insert panel (see Figure 2), drag a Spry Validation Text Field object into the second column of the first row. This will reserve the first columns for labels.

Figure 2. Choose the Forms category from the Insert panel menu.
When the Input Tag Accessibility Attributes dialog box opens, enter the following:
I prefer attaching
the <label> tag with the for attribute because I can then more easily move it to the
first column of the table. You'll notice that Dreamweaver automatically adds a
blue tab and border around the Spry object (see Figure 3), so you can quickly
invoke the proper Property inspector by selecting it.

Figure 3. Spry form validation object tab and border
A text field like this name field is perhaps the most basic of all validation: you're just making sure that some value was entered, because there is no set formula for a name. Now, bring in an email field that requires a specific format to be valid.
When the Input Tag Accessibility Attributes dialog box opens, enter the following:
<label> tag surrounding Email: and drag
it into the first column.Change the Invalid Format error message to Please enter a valid email address (see Figure 4). From the Preview states list, choose Required and change its error message to Please enter your email address.

Figure 4. Customizing an error message
Text fields are general purpose and will compose the bulk of your form elements. However, Dreamweaver also includes Spry form validation objects for other form elements, including select lists, radio buttons and check boxes. Next, add a check box to the form so you can see how that works.
From the Insert panel, drag a Spry Validation Checkbox into the second-to-last row, second column.
I prefer to group name, email and password fields (to be added later in this article) together. Of course, you can order your form elements however you like.
In the Input Tag Accessibility Attributes dialog box, enter the following:
Dreamweaver knows that the labels for check boxes typically appear after the element and this is the default choice for Position.
<span #sprycheckbox> tag with the Enforce Range
(multiple) option selected.The initial Spry form validation objects are in place. In the next section, you'll learn how to add two interrelated elements, with more sophisticated validation.
Dreamweaver CS4 includes several new form elements to round out the validation possibilities. In addition to the Spry Validation Radio Group, which allows you to enter a collection of radio buttons, the Spry Validation Password and Spry Validation Confirm objects are also new.
The Spry Validation Password object is especially powerful and allows web designers to specify the relative strength of a password. You can, for example, not only set the minimum and maximum number of characters, but also the minimum/maximum of letters, numbers, uppercase characters and even special characters, such as #, ?, &, and %. Once a solid password matching your criteria has been entered, you can make sure the user has it right with the Spry Validation Confirm object, which checks the value entered in one field against another.
When the Input Tag Accessibility Attributes dialog box opens, enter the following:
<label> tag surrounding Password: and
drag it into the first column.Note that every time you define a new validation requirement, a new entry is added to the Preview states list and a new error message made available (see Figure 5).

Figure 5. This shows the Spry password options.
You could continue to fine-tune your password requirements by entering values in any of the additional minimum/maximum fields. For this exercise, let's leave it as it is now and bring in the final Spry form validation elements, Spry Validation Confirm.
When the Input Tag Accessibility Attributes dialog box opens, enter the following:
<label> tag surrounding Re-enter your
password: and drag it into the first column.Each Spry form validation object includes a separate JavaScript and CSS file so you only have to upload the code you need. Should you need to remove any Spry object, Dreamweaver automatically removes the JavaScript include reference, so that file is not published as a dependent file.
With all the Spry form validation elements in place, there's just a bit more standard form work required. In this section, you'll add the Submit button and specify the form action.
When the Input Tag Accessibility Attributes dialog box opens, enter the following:
A separate label is not needed for the Submit button as the button contains its own label, which can be easily modified.
This might be a good time to test your page locally, which you can do by pressing F12 to preview it in your primary browser. If you want to see all the required error messages appear, press the Submit button without filling in any details. Refresh your browser page to start over and start to tab through the fields one at a time to verify that the Validate on Blur option was selected for all fields. Next, enter an improper address in the Email field or password with less than 6 or more than 12 characters to view those error messages.
Whenever you test your pages, it's always best to try every combination of data entry you can think of. If possible, set a web novice loose on your form. You'll quickly see how necessary form validations are—and how a full-featured approach to form validation is perfectly... valid.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
Joseph Lowery's books about the web and web-building tools are international bestsellers, having sold more than 400,000 copies worldwide in eleven different languages. His most recent book is the Dreamweaver CS3 Bible. Joseph is the author of the recently published CSS Hacks and Filters as well as the co-author of Dreamweaver 8 Recipes with Eric Ott. A well-known speaker, he was presented at Adobe (formerly Macromedia) conferences in the United States and Europe as well as user groups around the country. Joseph is currently the Vice President of Marketing for WebAssist.