Home arrow HTML arrow Page 3 - HTML5 Form Elements
HTML

HTML5 Form Elements


In this web development tutorial, I will show you how to use the new HTML5 form elements. Specifically, you will learn how to use the “placeholder”, “autofocus”, “email” and “url” attributes.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 5
June 08, 2011
TABLE OF CONTENTS:
  1. · HTML5 Form Elements
  2. · HTML5 autofocus attribute
  3. · HTML5 email and url attributes

print this article
SEARCH DEVARTICLES

HTML5 Form Elements - HTML5 email and url attributes
(Page 3 of 3 )

Dealing with email and web addresses: using the “email” and “url” attributes

As noted before, the “email” and “url” input attributes permit you to specify that a specific field in a form must be filled with an RFC compliant email address or URL. However, the functionality of these attributes scopes a wider spectrum than the purely semantic one, since some browsers like Opera 10 (at least at the time of this writing) will effectively validate the fields and won’t submit the corresponding form until you be nice and enter some valid data in them.

So, now that you have a clear idea on how the “email” and “url” attributes function, let me teach you to use them in a couple of concrete examples. Precisely, the following code bit shows how to utilize the former in a basic form. Take a look at it: 

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example using the 'email' input attribute</title>
<style type="text/css">
body {
    padding: 0;
    margin: 0;
    background-color: #fff;
    font: normal 0.9em Arial, Helvetica, sans-serif;
    color: #000;
}
h2 {
    margin: 0 0 10px 0;
    font-size: 2em;
    color: #666; 
}
p {
    margin: 0 0 15px 0;
    line-height: 1.3em;
}
/* main wrapper */
#wrapper {
    width: 780px;
    margin: 0 auto;
    background-color: #fff;
}
/* header, main and footer elements */
#header, #main, #footer {
    padding: 20px;
}
</style>
</head>
<body>
<div id="wrapper">
    <div id="header">
        <h2>Header section</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet.</p>
    </div>
    <div id="main">
        <h2>Sample form</h2>
        <form action="processform.php" method="post">
            <fieldset>
                <input name="email" type="email">
                <input type="submit" name="send" value="Send" />
            </fieldset>
        </form>
    </div>
    <div id="footer">
        <h2>Footer section</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet.</p>
    </div>
</div>
</body>
</html>

That was easy to code and read, wasn’t it? As you can see, using the “email” attribute is quite similar to coding a regular text box, although some user agents like Opera currently provides a an adequate validation, even if JavaScript has been disabled. Besides, if you’re curious and want to see how this attribute is displayed on most browsers, the following image will hopefully be quite demonstrative:  

There you have it. Since the “email” attribute is effectively rendered as a text box, feel free to start using it right away on your web pages, as your existing layouts won’t break up at all. On the other hand, a similar concept applies to the “url” attribute, since it’ll be degraded gracefully when displayed on a browser that doesn’t recognize it.

The example below shows how to use this attribute within the sample form previously created. Take a peek at it:   

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example using the 'url' input attribute</title>
<style type="text/css">
body {
    padding: 0;
    margin: 0;
    background-color: #fff;
    font: normal 0.9em Arial, Helvetica, sans-serif;
    color: #000;
}
h2 {
    margin: 0 0 10px 0;
    font-size: 2em;
    color: #666; 
}
p {
    margin: 0 0 15px 0;
    line-height: 1.3em;
}
/* main wrapper */
#wrapper {
    width: 780px;
    margin: 0 auto;
    background-color: #fff;
}
/* header, main and footer elements */
#header, #main, #footer {
    padding: 20px;
}
</style>
</head>
<body>
<div id="wrapper">
    <div id="header">
        <h2>Header section</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet.</p>
    </div>
    <div id="main">
        <h2>Sample form</h2>
        <form action="processform.php" method="post">
            <fieldset>
                <input type="url">
                <input type="submit" name="send" value="Send" />
            </fieldset>
        </form>
    </div>
    <div id="footer">
        <h2>Footer section</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet.</p>
    </div>
</div>
</body>
</html>

Without a doubt, the use of the “url” attribute is very straightforward. In consequence, you shouldn’t have major problems including it when coding your own HTML5 forms. Moreover, if you do so, the pertaining attribute will be rendered as a typical text box, similar to the one shown by the following screen capture:

Even when at first sight the utilization of attributes like the earlier “email” and “url” seems to be pretty pointless, this is nothing but a lame excuse. As more vendors start adding support to them in the hopefully (near) future, you’ll enjoy the benefits of delegating (at least to some extent) the complexities of client-side validation to browsers. Less work for you, more happiness for you visitors. In the end, a very appealing deal that you shouldn’t refuse to accept.   

Final thoughts

In this introductory part of this tutorial, I provided you with a quick overview on the new set of input attributes that come included with HTML5. As you saw in the earlier examples, taking advantage of their functionality (when possible, of course) is a straightforward process that can be mastered with minor effort.

So far, you learned how to utilize the “placeholder” and “autofocus” attributes, as well as how to indicate that a field in a form must contain a valid email address or URL, which has been hopefully quite an instructive experience. Nevertheless, the specification packages a few other attributes that permit to deal with numeric data, dates and even specify if a field is mandatory directly within the markup.

These additional attributes will be covered in the final installment of this article, so stick with my usual recommendation and don’t miss it!


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
HTML ARTICLES

- Does HTML5 Need a Main Element?
- Revisiting the HTML5 vs. Native Debate
- HTML5: Not for Phone Apps?
- HTML5 or Native?
- Job Hunting? Freelancer.com Lists This Quart...
- HTML5 in the News
- Report: HTML5 Mobile Performance Lags
- The Top HTML5 Audio Players
- Top HTML5 Video Tutorials
- HTML5: Reasons to Learn and Use It
- More of the Top Tutorials for HTML5 Forms
- MobileAppWizard Releases HTML5 App Builder
- HTML5 Boilerplate: Working with jQuery and M...
- HTML5 Boilerplate Introduction
- New API Platform for HTML5

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials