Conquer Forms with HTML5 and CSS3

Historically, forms have been a pain to style consistently cross-browser. They also require JavaScript to validate the inputs and lack speciic input types to deal with everyday information like telephone numbers, e-mail addresses, and URLs.

The good news is that HTML5 largely solves these common problems. Let's get familiar with the new HTML5 form features and see how they alleviate our traditional form-building burden.

Using HTML5 to code our forms brings an additional beneit when used for responsive designs; it once more allows us to trim our code base to provide the leanest possible pages for our users. For the browsers that don't support these new features, we have tools to patch them up and bring them in line.

In this chapter, we will learn how to use HTML5 to:

• Easily insert placeholder text into relevant form ields • Disable auto-completion of form ields where necessary • Set certain ields to be required before submission • Specify different input types such as e-mail, telephone number, and URL • Create number range sliders for easy value selection • Insert date and color pickers • Learn how we can use a regular expression to deine an allowed form value • Add a polyill to provide support for less capable browsers • Use CSS3 to easily and lexibly style an HTML5 form Conquer Forms with HTML5 and CSS3 HTML5 forms Here's the scenario: for our example And the winner isn't... responsive website. I've decided that I'd like people to be able to vent their own frustration at the turkeys that have been picking up the award gongs. We'll be adding a form that let's people tell us about the ilm they feel shouldn't have won, and the ilm they feel should have taken its place.

The following screenshot shows how our basic form looks, with just a little basic styling in Chrome (v16):

[ 238 ] Chapter 8

Besides standard form input ields and text areas, we have a number spinner, a range slider, and placeholder text for many of the ields. If we 'focus' (select) on that particular ield the placeholder text is removed and if we lose focus without entering anything (by clicking outside of the input box again) the placeholder text re-appears. Furthermore, looking at this page in Google's Chrome browser, if we go ahead and submit the form without entering anything, the following happens:

[ 239 ] Conquer Forms with HTML5 and CSS3

So besides a couple of visual lourishes (the slider and spinner) we have some client- side validation in place. As we've already noted, typically, to get a form working like this would require JavaScript of one sort or another.

However, the great news is that all these user interface elements (including the aforementioned slider, placeholder text, and spinner) and the form validation are all being handled natively with HTML5 and no JavaScript is being employed. Let's work through how the new form capabilities of HTML5 make this possible.

Understanding the component parts of HTML5 forms There's a lot going on in our HTML5 powered form, so let's break it down. The form has been given an ID to aid styling and then an HTML5 hgroup for the title and introductory text:

Oscar Redemption

Here's your chance to set the record straight: tell us what year the wrong film got nominated, and which film should have received a nod...

The three sections of the form are then wrapped in a fieldset with a legend:

About the offending film (part 1 of 3)

You can see from the previous code snippet that each input element of the form is also wrapped in a div with a label associated with each input. So far, so normal. However, within this irst input we've just stumbled upon our irst HTML5 form features. After common attributes of id, name, and type we have placeholder.

[ 240 ] Chapter 8 placeholder The placeholder attribute looks similar to the following:

placeholder="e.g. King Kong"

Placeholder text within form ields is such a common requirement that the folks creating HTML5 decided it should be built into the markup and supported by browsers. Simply include the placeholder attribute within your input and the value will be displayed by default until the ield gains focus. When it loses focus, if a value has not been entered, it will re-display the placeholder text.

After the placeholder attribute, in the previous code snippet, the next HTML5 form feature is the required attribute. required The required attribute looks similar to the following:

required aria-required="true"

In supporting HTML5 capable browsers, by adding the Boolean (meaning you simply include the attribute or not) attribute required within the input element, it indicates that a value is required. If the form is submitted without the ield containing the requisite information, a warning message should be displayed. The message displayed is speciic (both in content and styling) to both the browser and the input type used. In addition to the HTML5 required value, in our example we have also added the WAI-ARIA equivalent; aria-required="true". Unless there is a good reason not to, include this WAI-ARIA version of the required attribute to assist those using screen readers (if you remember, we looked at WAI-ARIA back in Chapter 4, HTML5 for Responsive Designs).

[ 241 ] Conquer Forms with HTML5 and CSS3

We've already seen what the required ield browser message looks like in Chrome. The following screenshot shows the same message in Firefox (9):

The required value can be used alongside many input types to ensure a value is entered. Notable exceptions are the range, color, button, and hidden input types as they almost always have a default value.

Another HTML5 form attribute that can be added to input ields is autofocus.

[ 242 ] Chapter 8 autofocus The HTML5 autofocus attribute allows a form to be loaded with a ield already focused (selected) ready for user input. The following code is an example of an input ield wrapped in a div with the autofocus attribute added at the end:

Be careful when using this attribute. Cross browser confusion can reign if multiple ields have the autofocus attribute added. For example, if multiple ields have autofocus added, in Chrome (v16) the last ield with the autofocus attributed is focused on page load. However, Firefox (v9) does the opposite with the irst autofocus ield selected. It's also worth considering that some users use the space bar to quickly skip down the content of a web page once it's loaded. On a page where a form has an autofocused input ield, it prevents this capability; instead it adds a space into the focused input ield. It's easy to see how that could be a source of frustration for users. autocomplete By default, most browsers aid user input by autocompleting the value of form ields where possible. Whilst the user can turn this preference on and off within the browser, we can now also indicate to the browser when we don't want a form or ield to allow auto-completion. This is useful not just for sensitive data (for example bank account numbers) but also if you want to ensure users pay attention and enter something by hand. For example, for many forms I complete, if a telephone number is required, I enter a 'spoof' telephone number. I know I'm not the only one that does that (doesn't everyone?) but I can ensure that users don't enter an autocompleted spoof number by setting the autocomplete attribute to off on the relevant input ield. The following is a code example of a ield with the autocomplete attribute set to off:

[ 243 ] Conquer Forms with HTML5 and CSS3

We can also set entire forms (but not ieldsets) to not autocomplete by using the attribute on the form itself. The following is a code example:

list (and the associated datalist element) This list attribute and the associated datalist element allow a number of selections to be presented to a user once they start entering a value in the ield. The following is a code example of the list attribute in use with an associated datalist wrapped in a div:

The value given in the list attribute (awards) refers to the id of the datalist. Doing this associates the datalist with the input ield. Although wrapping the options with a