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.

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

customError<br />

Purpose: Handle errors explicitly calculated and set by the application code<br />

Usage: Call setCustomValidity(message) to put a form control into the customError state<br />

Usage example: passwordConfirmationField.setCustomValidity("Password values do not match.");<br />

Details: For those cases where the built-in validity checks don’t apply, the custom validity errors can<br />

suffice. Application code should set a custom validity message whenever a field does not conform to<br />

semantic rules.<br />

One common use case for custom validity is when consistency between controls is not achieved, for<br />

example if password confirmation fields don’t match. (We’ll delve into this specific example in the<br />

“Practical Extras” section.) Whenever a custom validity message is set, the control will be invalid and<br />

return the customError constraint as true. To clear the error, simply call setCustomValidity("") on the<br />

control with an empty string value.<br />

Validation Fields and Functions<br />

Together, these eight constraints allow a developer to find out exactly why a given form control is failing<br />

a validation check. Or, if you don’t care which specific reason is causing the failure, simply access the<br />

Boolean value valid on the ValidityState; it is an aggregate of the other eight constraints. If all eight<br />

constraints return false, then the valid field will return true. There are a few other helpful fields and<br />

functions on the form controls which can assist you in programming for validation checking.<br />

The willValidate Attribute<br />

The willValidate attribute simply indicates whether validation will be checked on this form control at<br />

all. If any of the above constraints—e.g. the required attribute, pattern attribute, etc.—are set on the<br />

control, the willValidate field will let you know that validation checking is going to be enforced.<br />

The checkValidity Function<br />

The checkValidity function allows you to check validation on the form without any explicit user input.<br />

Normally, a form’s validation is checked whenever the user or script code submits the form. This<br />

function allows validation to be done at any time.<br />

■ Note Calling checkValidity on a form control doesn’t just check validation, it causes all resulting events and<br />

UI triggers to occur just as if the form had been submitted.<br />

The validationMessage Attribute<br />

This attribute isn’t yet supported by any current browser versions, but it might be by the time you read<br />

this. The validationMessage attribute lets you query programmatically a localized error message that the<br />

browser would display based on the current state of validation. For example, if a required field has no<br />

183

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

Saved successfully!

Ooh no, something went wrong!