23.05.2015 Views

Building the User Interface by Using HTML5: Organization ... - server

Building the User Interface by Using HTML5: Organization ... - server

Building the User Interface by Using HTML5: Organization ... - server

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

82 | Lesson 3<br />

Validation is <strong>the</strong> process of verifying that information entered or captured in a form is in <strong>the</strong><br />

correct format and usable before sending <strong>the</strong> data to <strong>the</strong> <strong>server</strong>. Some things that are verified<br />

during validation are:<br />

• Required fields are empty<br />

• Email addresses are valid<br />

• Dates are valid<br />

• Text does not appear in a numeric field or vice versa<br />

LICENSED PRODUCT NOT FOR RESALE<br />

CERTIFICATION READY<br />

How does <strong>HTML5</strong><br />

validate data entered into<br />

a form <strong>by</strong> a user?<br />

2.5<br />

<strong>Using</strong> HTML 4.01 and previous specifications, you often needed to use a lot of JavaScript<br />

or scripting in ano<strong>the</strong>r language to create custom validity rules and response messages, or to<br />

determine if an element is invalid.<br />

In <strong>HTML5</strong>, several of <strong>the</strong> input element types you learned about in <strong>the</strong> last section offer<br />

automatic validation of input, which means <strong>the</strong> browser checks <strong>the</strong> data <strong>the</strong> user inputs.<br />

This is referred to as client-side validation, because <strong>the</strong> input data is validated before submission<br />

to <strong>the</strong> <strong>server</strong>. (In cases in which <strong>the</strong> <strong>server</strong> validates data received from an input form,<br />

it’s referred to as <strong>server</strong>-side validation.) If <strong>the</strong> user enters <strong>the</strong> wrong type of data into a field,<br />

such as an email address in a field with <strong>the</strong> url attribute, <strong>the</strong> browser instructs <strong>the</strong> user to<br />

enter a valid URL. Let’s look at examples of <strong>the</strong> default error messages that are generated during<br />

automatic validation.<br />

The required attribute avoids <strong>the</strong> problem of empty fields that need to be populated. When<br />

a user skips a required field and clicks <strong>the</strong> submit button, an error message appears as shown<br />

in Figure 3-23. This example uses <strong>the</strong> Mozilla Firefox Web browser.<br />

Figure 3-23<br />

Error message in <strong>the</strong> Firefox<br />

browser for a required field<br />

<strong>HTML5</strong> also offers validation of Web addresses entered into fields with <strong>the</strong><br />

construct, and numbers entered into fields with <strong>the</strong> construct. If you use <strong>the</strong> min and max attributes with type="number",<br />

you will receive an error message from <strong>the</strong> browser if you enter a number that’s too small or<br />

too large.<br />

Finally, <strong>the</strong> pattern attribute prevents <strong>the</strong> user from entering data that doesn’t follow<br />

<strong>the</strong> pattern expression. In this example, <strong>the</strong> pattern attribute validates a five-digit zip<br />

code:<br />

<br />

Incorrectly entering data in <strong>the</strong> Zip Code field in <strong>the</strong> Firefox browser results in <strong>the</strong> error message<br />

shown in Figure 3-24.

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

Saved successfully!

Ooh no, something went wrong!