25.02.2013 Views

Peter Lubbers - Pro HTML 5 Programming

Pro HTML 5 Programming

Pro HTML 5 Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

You can do this by calling:<br />

valCheck.valid<br />

CHAPTER 7 ■ USING THE <strong>HTML</strong>5 FORMS API<br />

we can get a Boolean value which informs us whether or not all validity constraints are currently met on<br />

this particular form control. Think of the valid flag as a summary: if all eight constraints are passing, the<br />

valid flag will be true. Otherwise, if any of the validity constraints fail, the valid attribute will be false.<br />

■ Note The ValidityState object is a live object. Once you grab a reference to it, you can keep a hold of it and<br />

the validity checks it returns will update as needed when changes occur.<br />

As mentioned before, there are eight possible validity constraints on any given form element. Each<br />

can be accessed from the ValidityState by accessing the field with the appropriate name. Let’s look at<br />

what they mean, how they can be enforced on a form control, and how you can use the ValidityState to<br />

check for them:<br />

The valueMissing Constraint<br />

Purpose: Ensure that some value is set on this form control<br />

Usage: Set the required attribute on the form control to true<br />

Usage example: <br />

Details: If the required attribute is set on a form control, the control will be in an invalid state unless the<br />

user or a programmatic call sets some value to the field. For example, a blank text field will fail a required<br />

check, but will pass as soon as any text is entered. When blank, the valueMissing will return true.<br />

typeMismatch<br />

Purpose: Guarantee that the type of the value matches expectations (number, email, URL, and so on)<br />

Usage: Specify one of the appropriate type attributes on the form control<br />

Usage example: <br />

Details: Special form control types aren’t just for customized phone keyboards! If your browser can<br />

determine that the value entered into a form control doesn’t conform to the rules for that type—for<br />

example, an email address without an @ symbol—the browser can flag this control as having a type<br />

mismatch. Another example would be a number field that cannot parse to a valid number. In either case,<br />

the typeMismatch will return true.<br />

patternMismatch<br />

Purpose: Enforce any pattern rule set on a form control which details specific valid formats<br />

Usage: Set the pattern attribute on the form control with the appropriate pattern<br />

Usage example: <br />

181

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

Saved successfully!

Ooh no, something went wrong!