Coding a Guided Registration Form with jQuery

Techniques for building a usable registration form can be a major factor towards bringing in new users. When you’ve got an overly complicated form it can scare away so many first time visitors. But a great technique for keeping these people around is coding form hints into the layout.

In this tutorial I want to demonstrate how we can build such an interface with CSS3 and jQuery. We can embed hidden form tips which only display when the user has focused on a particular field. These hints can be used to denote syntax, such as the required length of usernames or passwords. But it’s a great usability tactic which livens up even the most dull websites.

Building the Webpage

We can start by coding just the standard HTML5 markup for the registration form. In my header I’ve got a reference to an external stylesheet styles.css along with a unique JavaScript file regform.js. We will use both of these to add our custom code, along with the most recent copy of jQuery.

To keep the layout simple I’ve placed the whole form inside a wrapper div with the margin: 0 auto; property setup. The form itself doesn’t tie into any backend – the action attribute points back to index.html passing data via POST.

To encapsulate the row areas I’ve divided the form into separate div classes. Each row contains a small label along with the input field on the right-hand side. After this I’m using another div with the class .note to hide a block element of form hints.

I’m using a number of additional attributes in the HTML to make the form easier for mobile users. autocomplete=”off” turns off the typical browser suggestion history which can be quite annoying at times. Also the tabindex attribute is setup +1 for each of the input fields in sequential order.

Stylin’ with CSS

The attached stylesheet is small but very manageable for this interface. First I’m using some really simple resets to remove margins, padding, and modulate font sizes between all browsers.

The labels and inputs inside each row are positioned carefully using inline-block. This will keep their display property with margins/padding while still holding the two elements next to each other. In the next block of code you’ll see I also setup 2 different classes for the text fields – one regular length and the other that’s a bit shorter. We can target both of these in jQuery later on.

I’m using some additional effects on the focus event for each input. CSS3 includes dynamic box-shadow effects which look great as you tab through the different inputs. For the select menus I’m trying to keep them much simpler so they just float next to each other.

Custom Buttons

The only other interesting bit of CSS code is designing the submit button. Thanks to such wide browser support we can build CSS3 gradients which require no images and can scale properly regardless of screen resolution. In our form design I’ve stuck with the green color scheme but it’s so easy to change the hexadecimal values tuned with your own layout.

One last note in the CSS file is how we’ve setup the small icons appearing on each note element. You can update these to display an error or success check mark as the user validates each field. For this demo it’s a lot easier just focusing on the informational fields without full validity. But the different classes are already included within the stylesheet if needed.

Dynamic jQuery

For the last part of our model let’s look into the regform.js file and see how to code these hints. I’ll break down the block into two sections for the focus and blur event handlers.

The selector I’m using will target both the regular inputs and the select menus. I haven’t included anything for the birthday section because it would change on each of the different day/month/year selections.

Each note has an internal span ID in the form of #note-ID where the ID value is the associated input field’s name. This is the value we setup in the variable thenote and we can use this to quickly select the internal span to fade in and out.

For the blur event handler I’ve added a bit more logic into the equation. The new variable currval gets the value of the currently targeted input before the user removes their focus. We can then check if the form is totally blank and re-hide the info field. Otherwise if the field has value we can keep the text displayed as a cautious reminder(makes proofreading handy!).

And that should be all we need to get this running! The JavaScript actually isn’t too confusing but for new scripters it may take some references to the jQuery docs. Feel free to test out the live demo and download my source code which you can play around with on your own.

Final Thoughts

Hopefully this tutorial can get you thinking about proper user experience and how to structure the flow of your website. Even a very small block of jQuery code can provide some enormous benefits to your average web layout. User forms are a very popular section to setup dynamic JavaScript events. If you have any similar ideas or suggestions feel free to share with us in the post discussion area below.

About the Author:

Jake is a freelance writer and frontend web developer. He can be found writing in many blogs on topics such as mobile interfaces, freelancing, jQuery, and Objective-C. Check out his other articles throughout Google and follow his tweets @jakerocheleau. Jake’s Google+ profile.

3 Responses

Comments are now closed on this post.

Eric,
July 5, 2012

Cool idea and nicely done. Only small thing I noticed, that may have either been intentional or just not noticed, is when you click the Gender drop-down and don’t actually make a selection, the ‘hint’ message does not go away. With all the other fields, if you don’t actually enter data and just move to another box, the ‘hint’ message goes away. Would be an easy fix, just wanted to point it out though.