
Part 2 Browser-based apps For a very long time developers were processing everything—form validation, file management, storage, messaging, and other vital application functionality— on the server. Server-side processing was a great idea for security reasons, lack of user processing power, and many other issues. There were workarounds through technologies such as Flash and Java, but the mobile market explosion revealed unanticipated limitations that HTML5 is aiming to fix. Thanks to major advances in JavaScript processing power and new W3C stan- dards, you can now perform server-side tasks through a user’s browser (aka client- side). Performing complex tasks through browsers saves tons of money on server costs, allows startups to easily create complex apps, and creates seemingly instant application responses during heavy load times. It also opens up a completely different thought process on application development and deployment to mobile and desktop. And they can both be done at the same time if you play your cards right. Many popular web applications use HTML5’s application features. Google Drive, for example, uses a new storage technology known as the Indexed Data- base API. You’ve probably also used HTML5’s WebSockets, forms, and many other features that we’ll be covering throughout this section. By the time you’ve completed part 2 (chapters 2–5), you’ll know enough to put together a small application with minimal server usage. Chapter 2 at a glance Topic Description, methods, and so on Page New input types1 HTML5 <input> element types ■ email 42 ■ tel 42 ■ number 46 ■ month 49 New input attributes1 HTML5 attributes on <input> elements ■ required 42 ■ pattern 49 ■ autofocus 43 ■ placeholder 42 ■ min and max 46 data-* attributes Storing key/value data in attributes on elements 46 valueAsNumber property Reading input values in numeric format 54 <output> element Displaying the output of calculations 47 Preventing validation Providing means of bypassing client-side validation ■ formnovalidate attribute 51 ■ formaction attribute 51 Constraint Validation API Client-side API for validation ■ setCustomValidity method 59 ■ validationMessage property 59 ■ invalid event 60 CSS3 pseudo-classes Styling invalid elements with CSS3 61 Backward compatibility Feature detection and unsupported browsers ■ Modernizr.js 63 ■ Polyfills 64 ■ Validation 65 1 Only the input types and attributes used or discussed in this chapter are listed here. For comprehensive cov- erage, visit mng.bz/wj56. Core API Look for this icon throughout the chapter to quickly locate the topics outlined in this table. Form creation: input widgets, data binding, and data validation This chapter covers ■ New HTML5 input types and attributes ■ data-* attributes, valueAsNumber property, and the <output> element ■ Constraint Validation API ■ Ways to bypass validation ■ CSS3 pseudo-classes ■ HTML5 feature detection with Modernizr and backward compatibility with polyfill As the web has matured, the need for a much richer set of form field types and widgets has emerged. Today’s users expect a consistent standard between web applications, par- ticularly when it comes to data validation. HTML5 meets this requirement with 13 new form input types, ranging from number spinners and sliders to date- and color-pickers. The standard also defines new attributes you can apply to <input> elements to enhance the functionality of your forms, including presentational attributes like placeholder and autofocus, as well as validation attributes such as required and pattern. You can even use a set of new CSS3 pseudo-classes to style valid or invalid form elements with zero JavaScript. And if you have advanced validation requirements you can’t provide natively, the new Constraint Validation API offers a standardized 37 38 CHAPTER 2 Form creation: input widgets, data binding, and data validation JavaScript API that can test for the validity of form fields, along with a new event you can use to detect an invalid data entry. In this chapter, you’ll implement all of these new features by building an order form for computer products. The form will use HTML5 validation to “sanitize” the input on the client side before it’s submitted. Why build this chapter’s order form? While working through this chapter’s sample application, you’ll learn to use: ■ New input types to provide more widgets with less coding ■ New input attributes to provide validation with less coding ■ data-* attributes to bind data to HTML elements ■ Constraint Validation API features to create custom validation tests We’ll get started by showing you a preview of the form and helping you get your pre- requisites in order. 2.1 Previewing the form and gathering prerequisites The order form you’ll build in this chapter, shown in figure 2.1, allows users to enter personal data, login details, and order and payment information. The form makes use of several new HTML5 features: ■ Form <input> element types (email, tel, number, and month) and attributes (required, pattern, autofocus, placeholder, and max and min) to provide users with bet- ter widgets and data validation when appropriate. ■ The data-* attributes to hold the price of each product, the valueAsNumber prop- erty to read input values in numeric format, and the <output> element to present subtotals and grand totals. ■ The formnovalidate and formaction attributes to bypass data validation and save an incomplete form. ■ The Constraint Validation API to perform custom validation and detect when the user attempts to submit the form with elements that are invalid, and CSS3 pseudo- class selectors to style invalid elements. ■ The Modernizr.js JavaScript library and polyfills to serve users whose browsers don’t support various HTML5 features. (Although we admit that Modernizr and poly- fills aren’t strictly HTML5 features, we recommend that you use them if you’re serious about developing HTML5 applications.) When you’ve finished, the order form will be functional in the latest versions of all the major browsers, although you may find varying levels of support for some features such as widgets for new input types and inline error messages for the Constraint Vali- dation API. But browser hiccups will become less and less an issue as support for the new features increases. Previewing the form and gathering prerequisites 39 The form itself comprises four main sections, each of which is grouped in a <fieldset> block: Contact details Requests the user’s name, email address, postal address, home and cell phone numbers, Skype name, and Twitter account. Login details Asks the user to enter their password twice (to ensure they enter it correctly). Order details Contains a table with three products; a product code, description, and price are provided for each. The user can enter a quantity value for each product, and the item and overall order total will be calculated dynamically. Payment details Requires a user to enter credit card details: the name on the card, the card number, the expiry date (month/year), and the CVV2 security code on the back of the card. Figure 2.1 The order form running in Google Chrome. The user is given two options when submitting the form: Submit Order or Save Order. The Submit Order button performs validation and processes a user’s order, whereas the Save Order button bypasses the validation and saves the details, so users can come back later and finish filling out their order. NOTE This chapter covers only the client-side portion of the order form. When the form is submitted, it makes a request to a URL. To perform further processing, you’ll need to implement the form on the server side using your choice of server-side language or framework (such as PHP or Ruby on Rails). The server-side aspect is outside the scope of this book. 2.1.1 Gathering the application prerequisites You’ll work with five files in this chapter: ■ An HTML document ■ A JavaScript source file ■ A CSS stylesheet ■ The Modernizr library ■ The month-picker polyfill script The stylesheet and polyfill are part of the chapter’s source code archive, but you’ll need to download the Modernizr library from its website at http://modernizr.com/. Rename the .js file to modernizr.js and place it, along with both the CSS file and monthpicker.js, in the application’s directory. 40 CHAPTER 2 Form creation: input widgets, data binding, and data validation TIP Modernizr offers two choices when you download the library—develop- ment or production builds. The development build contains the entire Mod- ernizr test suite and isn’t compressed or minified. If you’re in a hurry and don’t mind the large file size, use the development build. On the other hand, the production build allows you to configure which tests you want to include and will be compressed and minified to ensure a minimal file size. If you choose to use the production build, be sure to include the Input Attributes, Input Types, and Modernizr.load tests, because these are required in this chapter. You’ll learn more about Modernizr later in the chapter. With the preview and prerequisites out of the way, it’s time to start working on the form’s UI. 2.2 Building a form’s user interface The work in this section—building the UI—involves defining the HTML document structure, building the individual form sections, and allowing users to determine whether to save or submit form details. In this section, you’ll learn ■ How to provide users with widgets and data validation using HTML5 form <input> element types and attributes ■ How to store the price of each product with data-* attributes ■ How to present subtotals and grand totals using the <output> element ■ How to bypass form validation and save an incomplete form using the form attri- bute properties, formnovalidate and formaction We’ll walk you through the UI work in seven steps: ■ Step 1: Create index.html and load external files.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages36 Page
-
File Size-