Design Shack showcases inspiring web design, alongside resources and tutorials for you to succeed in the same way. It is carefully curated and edited by Josh Johnson and David Appleyard.
Digg.com is one of the most popular social networking sites, allowing you to discover and share the content all over the web. In this tutorial we are going to simulate their signup form, with unique features such as their dynamic tooltips that give you a hint on each field that is to be filled. The same approach will be adopted for displaying validation messages.
Before we get started, you may like to view a demo of the end result.
When an input field receives focus, a tooltip with a small blue icon is shown under it. On the other hand, if validation fails, an error message is shown in the same place. Both cases are shown on the screenshot below.
We are not going to recreate the entire form, but rather a few fields just to see how it works. Let’s take a closer look at the code sample. Each field row has a label, an input field, an info message and a validation message. Below you can see the HTML structure and CSS classes.
Note that, by default, both info and validation messages are hidden, which is set in tooltipContainer CSS class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | <fieldset class=“formContainer”> <h3>Join Now! It’s quick and easy.</h3> <div class=“rowContainer”> <label for=“txtFirstname”>Choose a username</label> <input id=“txtFirstname” type=“text” /> <div class=“tooltipContainer info”>Minimum 4 characters, maximum 15 characters. This is your handle on Digg so don’t make it too personal.</div> <div class=“tooltipContainer error”>Rats! Username must be between 4 and 15 characters.</div> </div> // other rows here </fieldset> body{ font-family:Arial, Sans-Serif; font-size:83%; } .formContainer { background-color: #F5EFC9; border:none; padding:30px; } .formContainer h3 { margin:0px; padding:0px 0px 10px 0px; font-size:135%; } .rowContainer { width:100%; overflow:hidden; padding-bottom:5px; height:34px; } .rowContainer label { width:140px; float:left; color: #758656; font-weight:bold; } .rowContainer input[type=“text”] { width:200px; } .tooltipContainer { height:16px; font-size:11px; color: #666666; display:none; float:none; background-repeat:no-repeat; background-position:left center; padding:0px 20px; } .info { background-image:url(‘info.gif’); } .error { background-image:url(‘error.gif’); } |
So far we have a static HTML structure and hidden messages waiting to be shown. We have to write a few lines of code to make them appear/disappear. Of course, this can be accomplished by pure JavaScript, but let’s involve jQuery. The reason for this is quite simple – less coding.
Like I mentioned at the beginning of this tutorial, an info tooltip should be shown each time an input field gets focus and hidden when it loses it. Turn this into jQuery code and you get this:
1 2 3 4 5 6 7 | $(document).ready(function(){ $(”.formContainer input[type=text]”).focus(function(){ $(this).parent().find(”.info”).css(“display”, “block”); }).blur(function(){ $(this).parent().find(”.info”).css(“display”, “none”); }); }); |
Simple, isn’t it? Now, let’s see how to handle validation messages. We are going to create fake validation just for the purpose of this tutorial. We’ll use jQuery again.
1 2 3 4 5 6 7 8 9 10 | function validateForm() { $(”.formContainer input[type=text]”).each(function(){ var text = $(this).attr(“value”); if (text == “”) { $(this).parent().find(”.error”).css(“display”, “block”); } }); } |
In essence, what the code above does is it shows a validation message for each field that the user hasn’t entered text in. Please keep in mind that this is not real validation, it just simulates what would happen if validation failed. You would have to implement your own logic, eg. check for mandatory fields, email address format and so on.
Ok, after pressing the sign-in button, validation messages will be shown. Signup form on Digg.com hides these messages and replaces them with info tooltips when the user starts to type. So we are missing a piece of code that will hide validation messages that were previously shown.
Let’s do it the dirty way. We’ll extend our jQuery code that controls the appearance of tooltips to hide corresponding validation messages each time an input field gets focus.
1 2 3 4 5 6 7 8 | $(document).ready(function(){ $(”.formContainer input[type=text]”).focus(function(){ $(this).parent().find(”.error”).css(“display”, “none”); $(this).parent().find(”.info”).css(“display”, “block”); }).blur(function(){ $(this).parent().find(”.info”).css(“display”, “none”); }); }); |
This way we have recreated the Digg.com signup form. You can check out live demo or download full source code.
You saw how to recreate the Digg.com signup form with simple CSS and just a few lines of jQuery code. However, there are a few pieces of advice I’d like to give you:
Tags: digg, form, JavaScript, signup, validation
Thanks Janko,
I’m just about implementing a sign up form, and it will help me a lot.
Good Work.
Best Regards.
Great article!
Don’t forget to set the password to “password” to hide the input.
Keep up the great work Janko!
This works really well – really good way to make my site form look much better.
Thanks, guys!
Marco, yeah I missed that, thanks :)
thanks janko, it looks great, however can you tell me how to set the password as said by marco, and also if i can use this form on my local site as I have yet to put my site online.
When i press sign in, i have no response. Do i need additional code?
Peter: you add to HTML structure and then add
.formContainer input[type=password]
to jQuery selector to include password fields as well.
Regarding the code-behind, you will need to create some logic to make it work.
Cool site.
Yes, This is what I want to do some weeks ago.
Thanks for your share.
Great site, this takes away 80% of the time id waste today trying to get this right on a proj im working on.
Quick Question
Is it possible to have the information that you are displaying bellow the input field appear in a tool tip that floats over the content. E.g. in case you had very limited predefined space for your form?
Thanks
Pete
how would I put validation on the email field to make sure it is a proper email address? Is there a simple way of adding that option?
thanks
No one was replaced by anybody. ,
There is a great deal to say on the role music plays as both a physical and a spiritual entity. ,
aaaaa
adasd
specifiedswarovski charm
swarovski charms for bracelet time there is a problem. You do not need fear, because you can return or exchange sswarovski heart pendantswarovski pendant beadsfor other same price of the product.
Bracelet in Sterling Silver with swarovski uk18ct Rolled Now withoutswarovski wholesale any worries and economic burden, she wasswarovski sale even enthusiastic about Every time she met beautiful she will without hesitation get them
Hey, great tutorial! Thanks.
Can you advice how can I change a text field to a text area for comments?
textarea does not work :|
Thanks