Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...

Advertise Here

Jim Nielsen on Jan 2nd 2013 with 89 Comments and 51 Reactions

Tutorial Details
Topic: HTML5 form validation, CSS3 Difficulty: Beginner Estimated Completion Time: 45 mins Let’s look at how to create a functional form which validates users’ data, client-side. With that done, we’ll cover prettying it up using CSS, including some CSS3! Republished Tutorial Every few weeks, we revisit some of our reader's favorite posts from throughout the history of the site. This tutorial was first published in November of 2011.

Step 1: Conceptualization Functionality
First we want to conceptualize what our form is going to look like and how it is going to function. For this example, let’s create a simple contact form that asks for the following information from the user: Name Email Website Message

1 of 21

01/08/2013 1:27 PM

Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...

We want to make sure the user is entering the information correctly. To accomplish this, we will use HTML5′s new client-side validation techniques. What about users who don’t have HTML5 capabilities? You can simply use server-side validation, but that will be beyond the scope of this article.

Step 2: Conceptualization Form
Let’s get an idea of what we want our form to look like by creating a rough mockup.

As you can see, the following elements make up our form: Form Title Required fields notification Form labels Form inputs Placeholder text Form field hints Submit Button Now that we’ve specified which elements make up our form, we can create the HTML markup.

Step 3: HTML Starter Code
Let’s create our basic HTML markup from the form concept we created. 001 <!DOCTYPE html>

2 of 21

01/08/2013 1:27 PM

. This is simply starter code for an HTML5 page.Bring Your Forms Up to Date With CSS3 and HTML5 Validation | http://webdesign.. So let’s start by creating the form header and our first input element: 001 002 003 004 005 006 007 008 009 010 <ul> <li> <h2>Contact Us</h2> <span class="required_notification">* Denotes Required Field</span </li> <li> <label for="name">Name:</label> <input type="text" name="name" /> </li> </ul> 3 of 21 01/08/2013 1:27 PM .tutsplus... since server-side validation will not be covered in this tutorial): 001 002 <form class="contact_form" action="" method="post" name="contact_form"> </form> Step 5: HTML Form Elements To keep our form content organized and structured. Step 4: HTML Form Let’s create the HTML form (we’ll leave the action method blank for now. etc) in a list. input. our HTML file will still appear blank in the browser.css" > </head> <body> </body> </html> Up to this point. 002 003 004 005 006 007 008 009 010 <html> <head> <meta charset="utf-8"> <title>HTML5 Contact Form</title> <link rel="stylesheet" media="screen" href="styles. we’ll wrap our form elements (label. 001 002 003 004 005 <li> <label for="email">Email:</label> <input type="text" name="email" /> <span class="form_hint">Proper format "name@something..Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd. we’re going to have formatting hints for the “email” and “website” fields. http://webdesign.. and give them a class so we can style them later.. Form Hints As seen in our mockup. So we’ll add our hints under the input fields where"</span> </li> 4 of 21 01/08/2013 1:27 PM .

remembering to wrap each section in a list item.tutsplus. The Remaining Input Elements Let’s go ahead and create our remaining form elements.. 001 002 003 004 005 006 007 008 009 010 011 012 <li> <label for="website">Website:</label> <input type="text" name="website" /> <span class="form_hint">Proper format ""</span> </li> <li> <label for="message">Message:</label> <textarea name="message" cols="40" rows="6" > </li> <li> <button class="submit" type="submit">Submit Form</button> </li> 5 of 21 01/08/2013 1:27 PM .Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd.

if you want to style your placeholder text.tutsplus. } ::-webkit-input-placeholder { color: blue. sorry). If you really need to have it supported across all browsers. 6 of 21 01/08/2013 1:27 PM . Step 6: Adding the Placeholder Attribute One of the first improvements HTML5 brings to web forms (one you’re probably already familiar with) is the ability to set the placeholder text.. This will help the user understand what they should enter in each field. Placeholder text is displayed when the input field is either empty or not in focus. Let’s add the placeholder attribute to our input elements.Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd.. } Support for the placeholder attribute is pretty well established in modern browsers (except IE9. there are some browser prefixes to help you: 001 002 003 004 005 006 :-moz-placeholder { color:" /> <input type="text" name="website" placeholder="" required/> Quick Tip: Style your placeholder Text Here’s a quick tip. there are some javascript solutions you could look into. 001 002 003 <input type="text" name="name" placeholder="John Doe" /> <input type="text" name="email" placeholder="john_doe@example.

required_notification {font-size: I’ll walk you through the rules: Remove :focus Style Webkit automatically adds some styling to input elements when they are in focus...contact_form label {font-family:Georgia. "Times New Rom . Step 7: Basic CSS Let’s add some basic CSS to give our form some structure..} 7 of 21 01/08/2013 1:27 PM . "Lucida Grande". "Lucida Sans Unicode" . http://webdesign.} Typographic Styles Let’s add some typographic styles to our form elements: 001 002 003 body {font: 14px/21px "Lucida Sans".tutsplus. Times. .form_hint.Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd.contact_form h2. . Since we’ll be adding our own styles.. we want to override these defaults: 001 *:focus {outline: none.

.contact_form ul { width:750px. margin:0px. } 8 of 21 01/08/2013 1:27 PM . border-bottom:1px solid #eee... http://webdesign. position:relative. } .Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd.tutsplus. padding:0px. List Styles Let’s style our list elements to give our form some structure: 001 002 003 004 005 006 007 008 009 010 011 012 ..contact_form li{ padding:12px. list-style-position:outside.

. Since this is not vital to key functionality. } 9 of 21 01/08/2013 1:27 PM . This adds some useful visual sectioning to our form. http://webdesign.tutsplus.. 001 002 003 . let’s add a slight border to the top and bottom sections of the form.contact_form li:last-child { border-bottom:1px solid #777.. as the names imply.contact_form li:first-child. Keep in mind that these CSS selectors are not supported in older browsers. the first and last elements in the <ul> list. we’re rewarding our those who use current browsers. .Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd. These select.. We can accomplish this by using the :first-child and :last-child selectors.

float:right. display:inline. 001 002 003 004 005 006 007 008 009 010 . http://webdesign.required_notification { This includes the heading tag and the notification that informs users that the asterisk (*) indicates required fields. Form Header Let’s style the header section of our form. } . display: inline.Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd..tutsplus...contact_form h2 { margin:0. } 10 of 21 01/08/2013 1:27 PM . margin:5px 0 0 0.

the ones used to collect user information..contact_form textarea {padding:8px.tutsplus. width:300px. margin-top: 3px. http://webdesign.contact_form label { width:150px. float:left. 001 002 003 004 005 006 007 008 009 010 011 012 013 014 .contact_form input { height:20px. Form Input Elements Let’s style all of our core form elements.. padding:5px 8px.Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd.. display:inline-block. width:220px..contact_form button {margin-left:156px. } .com/tutorials/site-elements/bring-your-forms-u. padding:3px.} .} 11 of 21 01/08/2013 1:27 PM . } .

http://webdesign. -o-border-radius: 3px. from(#68b12f background: -webkit-linear-gradient(top.contact_form input. #50911e). border-bottom: 1px solid #5b992b. 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 . .submit { background-color: #68b12f. background: -o-linear-gradient(top.contact_form input:focus. box-shadow: inset 0 1px 0 0 #9fd574.contact_form textarea:focus { background: #fff. background: linear-gradient(top. background: -webkit-gradient(linear. 0 10px 15px #eee inset. #50911e). left bottom. box-shadow: 0px 0px 3px #ccc.contact_form textarea { border:1px solid #aaa. -webkit-border-radius: 3px. #68b12f. background: -moz-linear-gradient(top.tutsplus. #68b12f. background: -ms-linear-gradient(top. left top.. let’s add some extra visual CSS styles. -ms-border-radius: 3px. #50911e). Now. -moz-border-radius: 3px. -webkit-box-shadow: 0 1px 0 0 #9fd574 inset . #68b12f. border-radius: 3px.. #50911e). #50911e). border-radius:2px. border: 1px solid #509111. ..Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd. Some of these are CSS3 styles that reward users who use modern browsers. } /* Button Style */ button. #68b12f. box-shadow: 0 0 3px #aaa.. 12 of 21 01/08/2013 1:27 PM . } . border:1px solid #555.

. box-shadow: 0 0 10px 5px #356b0b inset. -o-box-shadow: 0 0 10px 5px #356b0b inset. } button. http://webdesign. cursor: pointer. -ms-box-shadow: 0 0 10px 5px #356b0b inset.85. -webkit-box-shadow:0 0 10px 5px #356b0b inset . color: 13 of 21 01/08/2013 1:27 PM . -o-box-shadow: 0 1px 0 0 #9fd574 inset. padding: 6px 20px. text-align: center. .. font-weight: bold. We’ll make the field that is currently selected expand by adding some padding. 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 -moz-box-shadow: 0 1px 0 0 #9fd574 inset. } button.contact_form textarea:focus { /* add this to the alre padding-right:70px. -moz-box-shadow: 0 0 10px 5px #356b0b inset. } Step 8: Add Some Interactivity with CSS3 Let’s add a little bit of interactivity.submit:hover { opacity:.tutsplus. text-shadow: 0 -1px 0 #396715..contact_form input:focus.submit:active { border: 1px solid #20911e. 001 002 .Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd.. -ms-box-shadow: 0 1px 0 0 #9fd574 inset.

tutsplus. -webkit-transition: padding . 001 002 003 004 005 006 . 003 } Now for browsers that support it.25s. let’s make the expansion of the field a smooth transition using CSS3. 14 of 21 01/08/2013 1:27 PM .. textarea { /* add this to the already existing -moz-transition: padding ..contact_form input.25s.25s. a form cannot be submitted if a required field has not been filled out. } Step 9: The required Attribute in HTML5 Now it’s time for what we’ve all been waiting for: HTML5′s form handling tools.. http://webdesign. -o-transition: padding . . Adding the required attribute to any input/textarea element will tell the browser that a value is required before the form can be submitted..Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd. transition: padding .25s.

} 15 of 21 01/08/2013 1:27 PM . 001 002 003 input:required.contact_form input. we are going to add a red asterisk as a background image in each required field.. . textarea:required { background: #fff url(images/red_asterisk.. } Now we will use the CSS pseudo selector :required to target all the form elements with a required attribute. visually speaking. nothing happened by adding the required attribute. http://webdesign.tutsplus. We are going to style required fields using CSS. we will want to first add some padding on the right side of our input where the background image will be (this will prevent text overlap if the field entry is a long string): 001 002 003 .. So. 001 002 003 004 <input type="text" name="name" required /> <input type="text" name="email" required /> <input type="text" name="website" required /> <textarea name="message" cols="40" rows="6" required ></textarea> Step 10: Styling required Fields You’ll probably notice that. For this example. let’s go ahead and add the required attribute to all of our form elements (because we want them all to be filled out).com/tutorials/site-elements/bring-your-forms-u. I made a simple 16×16 pixel red asterisk icon in photoshop that will serve as the visual indicator of a required field.Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd.png) no-repeat 98% center.contact_form textarea { padding-right:30px.. To accomplish this.

marking the first field that is required and has no value... When the form is submitted. different browsers will do different things when a form using HTML5 elements is submitted.tutsplus. What happens upon submission? Right now. } Step 11: Understanding New HTML5 type Attributes and Client-Side Validation HTML5 validation works according to the type attribute that is set within the form You can see current browser support for the required attribute at quirksmode. Visual styling and support for these ‘bubble fields’ is quite broad. most browsers will prevent the form from being submitted and will display a “hint” to the user. Quick Tip: You can actually style the bubble message somewhat in webkit using the following: 001 002 003 ::-webkit-validation-bubble-message { padding: 1em. http://webdesign. Hopefully these behaviors will become standardized in the future.Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd. For years HTML only 16 of 21 01/08/2013 1:27 PM ..

An Example: The iPhone Apple’s iPhone detects the form types and dynamically changes the on-screen keyboard by providing context-aware characters. This is actually pretty amazing. As described eariler. So as long as the user enters at least one character in that field. there is a difference when it comes to the user interface. However. Step 12: Changing the type Attributes We already have our form fields set to the default type="text".. it will 17 of 21 01/08/2013 1:27 PM .tutsplus. Essentially you have backwards compatibility in all browsers on earth. http://webdesign.. it will simply default to type="text". for mobile browsers. By combining our input type attributes with the new required attribute. 001 002 <input type="email" name="email" placeholder="john_doe@example. all email addresses require the following symbols: “@” and “. the browser can now validate the form’s data not validate this form --> <input type="text" /> </form> Name Field Let’s look at our first field that asks the user for his/her name. For example. But now we want to change the type attribute on our email and website fields to their corresponding HTML5 type.. supported a handful of type attributes. If you wish to turn it off. If a user’s browser does not support the new type attributes..” So the iPhone provides those characters when the input type is specified to email. HTML5 validation is based on your type attributes and it is on by default. you can use the novalidate attribute like this: 001 002 003 004 <form novalidate> <-.Bring Your Forms Up to Date With CSS3 and HTML5 Validation |" required/> Step 13: HTML5 Validation As mentioned before. hooray! So what if the browser does actually support the new type attributes? For desktop browsers there is no visual difference (unless specified by custom CSS rules). such as type="email".com/tutorials/site-elements/bring-your-forms-u. we’ve added the type="text" attribute and the required attribute. This informs the web browser that this field is mandatory and it should validate the field as simply text. A type="text" field looks the same as a type="email" field. such as type="text" but with HTML5 there are a over a dozen new input types including email and url which we are going to use in our" required /> <input type="url" name="website" placeholder="http://johndoe. There is no specific markup required in order to activate form validation.

it appears as though the minimum requirement for most browsers is any character followed by a colon. red shadow. we only want to style the form as invalid when it is in border-color: #28921f. it is validated and green CSS styles are shown to indicate that fact. you could handle being more specific on with your input values in your server-side validation. As soon as a single character has been entered in the field. So. such as their email or home address. This is obviously not very limiting but it does prevent users from entering spaces or values that are entirely wrong. 001 002 003 004 005 . We’ll add a red border. green shadow. } Now when you focus on a form field. validate. and greed checkmark icon made in photoshop. if you entered “h:” then the field would validate.png) no-repeat 98% center. the red invalid styling is shown. 18 of 21 01/08/2013 1:27 PM . border-color: #b03535 } Now. Step 14: Introducing the HTML5 pattern Attribute Using the type="email" attribute as an example. 001 002 003 004 005 .. we can style our required fields that are either valid or invalid by adding :valid or :invalid to our CSS rules. Now. Now. For this example. In the example of the type="url" attribute. . box-shadow: 0 0 5px #d45252. let’s create the rules that indicate the field is valid. we’re going to talk about how to do that in HTML5.png) no-repeat 98% center. We’ll add a green border. First.contact_form textarea:required:valid { background: #fff url(images/valid. Email and URL Fields Our CSS styles and validation rules are already applied to the email field because we set the type and required attributes earlier. however. it appears that most browsers validate that field as *@* (any character + the “@” symbol + any character).contact_form textarea:focus:invalid { /* when background: #fff url(images/invalid.contact_form input:required:valid. . If you remember. This is not extremely helpful but it does prevent users from entering irrelevant information.. let’s style fields that are invalid. This will be applied to all valid fields whether they are in focus or not. we used :required in our CSS to style all input elements with a required attribute.Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd. Now we will create our own CSS to style field inputs that are considered valid and invalid by the browser. and red icon created in photoshop to indicate the invalid field.tutsplus.. box-shadow: 0 0 5px #5cd053. http://webdesign.contact_form input:focus:invalid.

/* hints stay above all other elements */ position: absolute. border-radius: 3px 3px 3px 3px. Then we just use positioning to get it where we want it. 001 002 003 004 005 006 007 008 009 010 .. So our HTML now looks like this: 001 <input type="url" name="website" placeholder="http://johndoe. This can be done using images. because they are always considered invalid until the proper information is entered in. rather than the browser default.. http://webdesign. These regular expression patterns can be confusing at first. Step 15: Form Field Hints (CSS) Now let’s style our form hints that tell the user the format they should use when entering their information. z-index: Normally we would use the HTML Unicode format to display these in our HTML (as shown in the image above). However. } We set display:none because we are only going to show the hints when the user focuses on the input" required Now our field will only accept values that start with “http://” or “https://” and one additional character.form_hint::before { 19 of 21 01/08/2013 1:27 PM .. This expression is used. color: white.form_hint { background: #d45252.tutsplus. your forms will be open to a whole new world.Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd. Because it is purely a presentational element that is not vital to the page’s functionality. /* allows proper formatting if hint is two lines */ display: none. We can do this by using one of the unicode geometric shapes. but in our case we are going to do it using pure CSS. we have to use the triangle’s corresponding escaped unicode when using the content:"" rule. we are going to add a small triangle that points left using the ::before pseudo selector. The pattern Attribute The pattern attribute accepts a javascript regular expression. to validate the field’s value.. 001 . but once you take the time to learn them. because we will be using the ::before CSS selector. Using the ::before Selector Now we want to add a little triangle to our hint boxes that help direct and guide the eye. margin-left:8px. padding: 1px 6px. We also set our tooltips to default to our red invalid color.

HTML5 validation is coming closer to replacing client-side validation in helping users properly fill out their online forms. The adjacent selector (x + y) selects the element that is immediately preceded by the former element.form_hint::before {color:#28921f. we are going to use the CSS adjacent selector to show and hide our form field top:1px.contact_form input:required:valid + . /* left point triangle in escaped unicode */ color:#d45252. HTML5 validation still does not replace server-side validation. we can use this selector to show/hide the tooltips.form_hint {background: #28921f.contact_form input:required:valid + .. Step 16: Sit Back and Admire Your Beautiful HTML5 Form Go ahead and take a look at your final product! Conclusion As you can see..} /* chang As you can see from the CSS. 001 002 003 .tutsplus. Since our field hints come right after our input fields in our HTML. http://webdesign.} /* change . the new HTML5 form features are pretty neat! Everything is backwards compatible so incorporating these new features into your website won’t break anything. position: absolute. Thanks for reading! ERROR 20 of 21 01/08/2013 1:27 PM . For the time being. left:-6px.contact_form input:focus + . However. we also set the form hints to change colors along with the input’s border when a field is valid or invalid. } Using the + Adjacent Selector Finally..} .form_hint {display: inline.Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd. 002 003 004 005 006 007 content: "5C0". it’s best to use both methods when handling user-submitted information.

and even physics! Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. http://webdesign. He loves acquiring new knowledge and hopes to someday develop competency in a wide-array of disciplines including programming. Tags: css3formhtml5validation By Jim Nielsen Jim Nielsen considers himself a web designer at heart..Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd.. Find out more 21 of 21 01/08/2013 1:27 PM . though he often dabbles in other areas such as print and identity design.