02.06.2013 Views

JavaScript & jQuery: The Missing Manual ... - Robert Guajardo

JavaScript & jQuery: The Missing Manual ... - Robert Guajardo

JavaScript & jQuery: The Missing Manual ... - Robert Guajardo

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Congratulations—you’ve just added validation to your form using the basic<br />

method discussed on page 281. Next, you’ll add another validation rule for the<br />

“date of birth” field.<br />

Note: If you don’t see an error message and instead get a page with the headline “Form Processed,” the<br />

validation didn’t work and the form was submitted anyway. Go over steps 1–4 again to make sure you<br />

didn’t make any typos.<br />

6. Locate the HTML for the date of birth field——and add class and title attributes so the tag looks like this (changes<br />

are in bold):<br />

<br />

Because you didn’t add the required class, filling out this field is optional. However,<br />

if the visitor does type anything into the field, the class=“date” tells the<br />

plug-in that the input must be formatted like a date. You use the title attribute<br />

again to hold the error message if this field isn’t valid. Save the page and try it<br />

out in a web browser—type something like kjsdf in the date of birth field and<br />

try to submit the form.<br />

Note: If you did want to require that the visitor fill out the date of birth field and that enter a valid date,<br />

just add required to the class attribute. Just make sure date and required are separated by a space:<br />

class="date required"<br />

You can use the same technique for validating a menu ( tag).<br />

www.it-ebooks.info<br />

chapter 9: enhancing web forms<br />

Validation Tutorial<br />

Figure 9-8:<br />

Don’t worry about<br />

the appearance of the<br />

error message just<br />

yet. You’ll learn how<br />

to format errors on<br />

page 299.<br />

293

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!