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: