Professional Documents
Culture Documents
Bring Your Forms Up To Date With CSS3 and HTML5 Validation - Webdesigntuts+
Bring Your Forms Up To Date With CSS3 and HTML5 Validation - Webdesigntuts+
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
Advertise Here
Tutorial Details
Topic: HTML5 form validation, CSS3 Difficulty: Beginner Estimated Completion Time: 45 mins Lets look at how to create a functional form which validates users data, client-side. With that done, well 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.
1 of 21
01/08/2013 1:27 PM
Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
We want to make sure the user is entering the information correctly. To accomplish this, we will use HTML5s new client-side validation techniques. What about users who dont have HTML5 capabilities? You can simply use server-side validation, but that will be beyond the scope of this article.
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 weve specified which elements make up our form, we can create the HTML markup.
2 of 21
01/08/2013 1:27 PM
Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
<html> <head> <meta charset="utf-8"> <title>HTML5 Contact Form</title> <link rel="stylesheet" media="screen" href="styles.css" > </head> <body> </body> </html>
Up to this point, our HTML file will still appear blank in the browser. This is simply starter code for an HTML5 page.
3 of 21
01/08/2013 1:27 PM
Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
Form Hints
As seen in our mockup, were going to have formatting hints for the email and website fields. So well add our hints under the input fields where necessary, and give them a class so we can style them later. 001 002 003 004 005 <li> <label for="email">Email:</label> <input type="text" name="email" /> <span class="form_hint">Proper format "name@something.com"</span> </li>
4 of 21
01/08/2013 1:27 PM
Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
5 of 21
01/08/2013 1:27 PM
Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
Support for the placeholder attribute is pretty well established in modern browsers (except IE9, sorry). If you really need to have it supported across all browsers, there are some javascript solutions you could look into.
6 of 21
01/08/2013 1:27 PM
Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
Typographic Styles
Lets add some typographic styles to our form elements: 001 002 003
body {font: 14px/21px "Lucida Sans", "Lucida Grande", "Lucida Sans Unicode" .contact_form h2, .contact_form label {font-family:Georgia, Times, "Times New Rom .form_hint, .required_notification {font-size: 11px;}
7 of 21
01/08/2013 1:27 PM
Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
List Styles
Lets style our list elements to give our form some structure: 001 002 003 004 005 006 007 008 009 010 011 012 .contact_form ul { width:750px; list-style-type:none; list-style-position:outside; margin:0px; padding:0px; } .contact_form li{ padding:12px; border-bottom:1px solid #eee; position:relative; }
8 of 21
01/08/2013 1:27 PM
Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
Also, lets add a slight border to the top and bottom sections of the form. We can accomplish this by using the :first-child and :last-child selectors. These select, as the names imply, the first and last elements in the <ul> list. This adds some useful visual sectioning to our form. Keep in mind that these CSS selectors are not supported in older browsers. Since this is not vital to key functionality, were rewarding our those who use current browsers. 001 002 003 .contact_form li:first-child, .contact_form li:last-child { border-bottom:1px solid #777; }
9 of 21
01/08/2013 1:27 PM
Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
Form Header
Lets style the header section of our form. This includes the heading tag and the notification that informs users that the asterisk (*) indicates required fields. 001 002 003 004 005 006 007 008 009 010 .contact_form h2 { margin:0; display: inline; } .required_notification { color:#d45252; margin:5px 0 0 0; display:inline; float:right; }
10 of 21
01/08/2013 1:27 PM
Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
11 of 21
01/08/2013 1:27 PM
Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
Now, lets add some extra visual CSS styles. Some of these are CSS3 styles that reward users who use modern browsers. 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 .contact_form input, .contact_form textarea { border:1px solid #aaa; box-shadow: 0px 0px 3px #ccc, 0 10px 15px #eee inset; border-radius:2px; } .contact_form input:focus, .contact_form textarea:focus { background: #fff; border:1px solid #555; box-shadow: 0 0 3px #aaa; } /* Button Style */ button.submit { background-color: #68b12f; background: -webkit-gradient(linear, left top, left bottom, from(#68b12f background: -webkit-linear-gradient(top, #68b12f, #50911e); background: -moz-linear-gradient(top, #68b12f, #50911e); background: -ms-linear-gradient(top, #68b12f, #50911e); background: -o-linear-gradient(top, #68b12f, #50911e); background: linear-gradient(top, #68b12f, #50911e); border: 1px solid #509111; border-bottom: 1px solid #5b992b; border-radius: 3px; -webkit-border-radius: 3px; -moz-border-radius: 3px; -ms-border-radius: 3px; -o-border-radius: 3px; box-shadow: inset 0 1px 0 0 #9fd574; -webkit-box-shadow: 0 1px 0 0 #9fd574 inset ;
12 of 21
01/08/2013 1:27 PM
Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
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; -ms-box-shadow: 0 1px 0 0 #9fd574 inset; -o-box-shadow: 0 1px 0 0 #9fd574 inset; color: white; font-weight: bold; padding: 6px 20px; text-align: center; text-shadow: 0 -1px 0 #396715; } button.submit:hover { opacity:.85; cursor: pointer; } button.submit:active { border: 1px solid #20911e; box-shadow: 0 0 10px 5px #356b0b inset; -webkit-box-shadow:0 0 10px 5px #356b0b inset ; -moz-box-shadow: 0 0 10px 5px #356b0b inset; -ms-box-shadow: 0 0 10px 5px #356b0b inset; -o-box-shadow: 0 0 10px 5px #356b0b inset; }
13 of 21
01/08/2013 1:27 PM
Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
003
Now for browsers that support it, lets make the expansion of the field a smooth transition using CSS3. 001 002 003 004 005 006
.contact_form input, .contact_form textarea { /* add this to the already existing -moz-transition: padding .25s; -webkit-transition: padding .25s; -o-transition: padding .25s; transition: padding .25s; }
14 of 21
01/08/2013 1:27 PM
Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
So, lets go ahead and add the required attribute to all of our form elements (because we want them all to be filled out). 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>
Now we will use the CSS pseudo selector :required to target all the form elements with a required attribute. I made a simple 1616 pixel red asterisk icon in photoshop that will serve as the visual indicator of a required field. 001 002 003 input:required, textarea:required { background: #fff url(images/red_asterisk.png) no-repeat 98% center; }
15 of 21
01/08/2013 1:27 PM
Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
You can see current browser support for the required attribute at quirksmode.
Quick Tip:
You can actually style the bubble message somewhat in webkit using the following: 001 002 003 ::-webkit-validation-bubble-message { padding: 1em; }
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 fields. For years HTML only
16 of 21
01/08/2013 1:27 PM
Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
supported a handful of type attributes, 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 form. By combining our input type attributes with the new required attribute, the browser can now validate the forms data client-side. If a users browser does not support the new type attributes, such as type="email", it will simply default to type="text". This is actually pretty amazing. Essentially you have backwards compatibility in all browsers on earth, 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). A type="text" field looks the same as a type="email" field. However, for mobile browsers, there is a difference when it comes to the user interface.
Name Field
Lets look at our first field that asks the user for his/her name. As described eariler, weve 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. So as long as the user enters at least one character in that field, it will
17 of 21
01/08/2013 1:27 PM
Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
validate. Now we will create our own CSS to style field inputs that are considered valid and invalid by the browser. If you remember, we used :required in our CSS to style all input elements with a required attribute. Now, we can style our required fields that are either valid or invalid by adding :valid or :invalid to our CSS rules. First, lets style fields that are invalid. For this example, we only want to style the form as invalid when it is in focus. Well add a red border, red shadow, and red icon created in photoshop to indicate the invalid field. 001 002 003 004 005
.contact_form input:focus:invalid, .contact_form textarea:focus:invalid { /* when background: #fff url(images/invalid.png) no-repeat 98% center; box-shadow: 0 0 5px #d45252; border-color: #b03535 }
Now, lets create the rules that indicate the field is valid. Well add a green border, green shadow, and greed checkmark icon made in photoshop. This will be applied to all valid fields whether they are in focus or not. 001 002 003 004 005 .contact_form input:required:valid, .contact_form textarea:required:valid { background: #fff url(images/valid.png) no-repeat 98% center; box-shadow: 0 0 5px #5cd053; border-color: #28921f; }
Now when you focus on a form field, the red invalid styling is shown. As soon as a single character has been entered in the field, it is validated and green CSS styles are shown to indicate that fact.
18 of 21
01/08/2013 1:27 PM
Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
Now our field will only accept values that start with http:// or https:// and one additional character. These regular expression patterns can be confusing at first, but once you take the time to learn them, your forms will be open to a whole new world.
We set display:none because we are only going to show the hints when the user focuses on the input field. We also set our tooltips to default to our red invalid color, because they are always considered invalid until the proper information is entered in.
Normally we would use the HTML Unicode format to display these in our HTML (as shown in the image above). However, because we will be using the ::before CSS selector, we have to use the triangles corresponding escaped unicode when using the content:"" rule. Then we just use positioning to get it where we want it. 001 .form_hint::before {
19 of 21
01/08/2013 1:27 PM
Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
content: "\25C0"; /* left point triangle in escaped unicode */ color:#d45252; position: absolute; top:1px; left:-6px; }
.contact_form input:focus + .form_hint {display: inline;} .contact_form input:required:valid + .form_hint {background: #28921f;} /* change .contact_form input:required:valid + .form_hint::before {color:#28921f;} /* chang
As you can see from the CSS, we also set the form hints to change colors along with the inputs border when a field is valid or invalid.
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, the new HTML5 form features are pretty neat! Everything is backwards compatible so incorporating these new features into your website wont break anything. HTML5 validation is coming closer to replacing client-side validation in helping users properly fill out their online forms. However, HTML5 validation still does not replace server-side validation. For the time being, its best to use both methods when handling user-submitted information. Thanks for reading!
ERROR
20 of 21
01/08/2013 1:27 PM
Bring Your Forms Up to Date With CSS3 and HTML5 Validation | Webd...
http://webdesign.tutsplus.com/tutorials/site-elements/bring-your-forms-u...
Tags: css3formhtml5validation
By Jim Nielsen
Jim Nielsen considers himself a web designer at heart, though he often dabbles in other areas such as print and identity design. He loves acquiring new knowledge and hopes to someday develop competency in a wide-array of disciplines including programming, mathematics, and even physics!
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
21 of 21
01/08/2013 1:27 PM