Fancy Sliding Form with jQuery

Today we are going to create a fancy sliding form that shows some validation feedback to the user after each step. This form saves a lot of space and is […]

Today we are going to create a fancy sliding form that shows some validation feedback to the user after each step. This form saves a lot of space and is easy to access – it basically works like a slide show, just that we have fieldsets of a form instead of images.

So let’s start!

Tiny break: 📬 Want to stay up to date with frontend and trends in web design? Subscribe and get our Collective newsletter twice a tweek.

The Markup

The HTML will consist of a wrapper container where we will place a form with fieldsets. Each fieldset will be a step in our sliding form:

<h1>Fancy Sliding Form with jQuery</h1>
<div id="wrapper">
	<div id="steps">
		<form id="formElem" name="formElem" action="" method="post">
			<fieldset class="step">
				<legend>Account</legend>
				<p>
					<label for="username">User name</label>
					<input id="username" name="username" />
				</p>
				<p>
					<label for="email">Email</label>
					<input id="email" name="email" type="email" />
				</p>
				<p>
					<label for="password">Password</label>
					<input id="password" name="password" type="password" />
				</p>
			</fieldset>
			<fieldset>
			...
			</fieldset>
		</form>
	</div>
	<div id="navigation" style="display:none;">
		<ul>
			<li class="selected">
				<a href="#">Account</a>
			</li>
			<li>
				<a href="#">Personal Details</a>
			</li>
			<li>
				<a href="#">Payment</a>
			</li>
			<li>
				<a href="#">Settings</a>
			</li>
			<li>
				<a href="#">Confirm</a>
			</li>
		</ul>
	</div>
</div>

The navigation with all the links to the steps will be an unordered list that is initially hidden. We will show it in our JavaScript function.
Let’s look at the style.

The CSS

The main wrapper and the steps container will have the following style:

#wrapper{
    -moz-box-shadow:0px 0px 3px #aaa;
    -webkit-box-shadow:0px 0px 3px #aaa;
    box-shadow:0px 0px 3px #aaa;
    -moz-border-radius:10px;
    -webkit-border-radius:10px;
    border-radius:10px;
    border:2px solid #fff;
    background-color:#f9f9f9;
    width:600px;
    overflow:hidden;
}
#steps{
    width:600px;
    overflow:hidden;
}
.step{
    float:left;
    width:600px;
}

The class step will be given to each fieldset. Let’s style the navigation:

#navigation{
    height:45px;
    background-color:#e9e9e9;
    border-top:1px solid #fff;
    -moz-border-radius:0px 0px 10px 10px;
    -webkit-border-bottom-left-radius:10px;
    -webkit-border-bottom-right-radius:10px;
    border-bottom-left-radius:10px;
    border-bottom-right-radius:10px;
}
#navigation ul{
    list-style:none;
	float:left;
	margin-left:22px;
}
#navigation ul li{
	float:left;
    border-right:1px solid #ccc;
    border-left:1px solid #ccc;
    position:relative;
	margin:0px 2px;
}

The single link elements will have a neat CSS3 gradient as background:

#navigation ul li a{
    display:block;
    height:45px;
    background-color:#444;
    color:#777;
    outline:none;
    font-weight:bold;
    text-decoration:none;
    line-height:45px;
    padding:0px 20px;
    border-right:1px solid #fff;
    border-left:1px solid #fff;
    background:#f0f0f0;
    background:
        -webkit-gradient(
        linear,
        left bottom,
        left top,
        color-stop(0.09, rgb(240,240,240)),
        color-stop(0.55, rgb(227,227,227)),
        color-stop(0.78, rgb(240,240,240))
        );
    background:
        -moz-linear-gradient(
        center bottom,
        rgb(240,240,240) 9%,
        rgb(227,227,227) 55%,
        rgb(240,240,240) 78%
        )
}
#navigation ul li a:hover,
#navigation ul li.selected a{
    background:#d8d8d8;
    color:#666;
    text-shadow:1px 1px 1px #fff;
}

When a step get’s validated, we will either add a span indicating that everything is fine, or a span that shows that something is wrong in the form step:

span.checked{
    background:transparent url(../images/checked.png) no-repeat top left;
    position:absolute;
    top:0px;
    left:1px;
    width:20px;
    height:20px;
}
span.error{
    background:transparent url(../images/error.png) no-repeat top left;
    position:absolute;
    top:0px;
    left:1px;
    width:20px;
    height:20px;
}

The styles for the form elements look as follows:

#steps form fieldset{
    border:none;
    padding-bottom:20px;
}
#steps form legend{
    text-align:left;
    background-color:#f0f0f0;
    color:#666;
    font-size:24px;
    text-shadow:1px 1px 1px #fff;
    font-weight:bold;
    float:left;
    width:590px;
    padding:5px 0px 5px 10px;
    margin:10px 0px;
    border-bottom:1px solid #fff;
    border-top:1px solid #d9d9d9;
}
#steps form p{
    float:left;
    clear:both;
    margin:5px 0px;
    background-color:#f4f4f4;
    border:1px solid #fff;
    width:400px;
    padding:10px;
    margin-left:100px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-box-shadow:0px 0px 3px #aaa;
    -webkit-box-shadow:0px 0px 3px #aaa;
    box-shadow:0px 0px 3px #aaa;
}
#steps form p label{
    width:160px;
    float:left;
    text-align:right;
    margin-right:15px;
    line-height:26px;
    color:#666;
    text-shadow:1px 1px 1px #fff;
    font-weight:bold;
}
#steps form input:not([type=radio]),
#steps form textarea,
#steps form select{
    background: #ffffff;
    border: 1px solid #ddd;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    border-radius: 3px;
    outline: none;
    padding: 5px;
    width: 200px;
    float:left;
}
#steps form input:focus{
    -moz-box-shadow:0px 0px 3px #aaa;
    -webkit-box-shadow:0px 0px 3px #aaa;
    box-shadow:0px 0px 3px #aaa;
    background-color:#FFFEEF;
}
#steps form p.submit{
    background:none;
    border:none;
    -moz-box-shadow:none;
    -webkit-box-shadow:none;
    box-shadow:none;
}
#steps form button {
	border:none;
	outline:none;
    -moz-border-radius: 10px;
    -webkit-border-radius: 10px;
    border-radius: 10px;
    color: #ffffff;
    display: block;
    cursor:pointer;
    margin: 0px auto;
    clear:both;
    padding: 7px 25px;
    text-shadow: 0 1px 1px #777;
    font-weight:bold;
    font-family:"Century Gothic", Helvetica, sans-serif;
    font-size:22px;
    -moz-box-shadow:0px 0px 3px #aaa;
    -webkit-box-shadow:0px 0px 3px #aaa;
    box-shadow:0px 0px 3px #aaa;
    background:#4797ED;
}
#steps form button:hover {
    background:#d8d8d8;
    color:#666;
    text-shadow:1px 1px 1px #fff;
}

And now, let’s add some JavaScript magic!

The JavaScript

In our function we will first take care of the display of the slider. For that we need to calculate the width of the elements inside. We will also take care of the sliding when we tab through the inputs. When the user hits tab when he is in the last input of a fieldset, we make the next fieldset slide into place and focus on the first field.

The validation of the form is based on all inputs being required fields. If we slide to the next fieldset and did not fill out all the inputs of the previous one, we will mark the missing inputs with a red background and add our little error span to the navigation item. If everything was fine, we will add the span with the check mark so that the user knows, the step was correct. We will not allow the user to submit the form if one of the steps contains errors.

$(function() {
	/*
	number of fieldsets
	*/
	var fieldsetCount = $('#formElem').children().length;

	/*
	current position of fieldset / navigation link
	*/
	var current 	= 1;

	/*
	sum and save the widths of each one of the fieldsets
	set the final sum as the total width of the steps element
	*/
	var stepsWidth	= 0;
    var widths 		= new Array();
	$('#steps .step').each(function(i){
        var $step 		= $(this);
		widths[i]  		= stepsWidth;
        stepsWidth	 	+= $step.width();
    });
	$('#steps').width(stepsWidth);

	/*
	to avoid problems in IE, focus the first input of the form
	*/
	$('#formElem').children(':first').find(':input:first').focus();	

	/*
	show the navigation bar
	*/
	$('#navigation').show();

	/*
	when clicking on a navigation link
	the form slides to the corresponding fieldset
	*/
    $('#navigation a').bind('click',function(e){
		var $this	= $(this);
		var prev	= current;
		$this.closest('ul').find('li').removeClass('selected');
        $this.parent().addClass('selected');
		/*
		we store the position of the link
		in the current variable
		*/
		current = $this.parent().index() + 1;
		/*
		animate / slide to the next or to the corresponding
		fieldset. The order of the links in the navigation
		is the order of the fieldsets.
		Also, after sliding, we trigger the focus on the first
		input element of the new fieldset
		If we clicked on the last link (confirmation), then we validate
		all the fieldsets, otherwise we validate the previous one
		before the form slided
		*/
        $('#steps').stop().animate({
            marginLeft: '-' + widths[current-1] + 'px'
        },500,function(){
			if(current == fieldsetCount)
				validateSteps();
			else
				validateStep(prev);
			$('#formElem').children(':nth-child('+ parseInt(current) +')').find(':input:first').focus();
		});
        e.preventDefault();
    });

	/*
	clicking on the tab (on the last input of each fieldset), makes the form
	slide to the next step
	*/
	$('#formElem > fieldset').each(function(){
		var $fieldset = $(this);
		$fieldset.children(':last').find(':input').keydown(function(e){
			if (e.which == 9){
				$('#navigation li:nth-child(' + (parseInt(current)+1) + ') a').click();
				/* force the blur for validation */
				$(this).blur();
				e.preventDefault();
			}
		});
	});

	/*
	validates errors on all the fieldsets
	records if the form has errors in $('#formElem').data()
	*/
	function validateSteps(){
		var FormErrors = false;
		for(var i = 1; i < fieldsetCount; ++i){
			var error = validateStep(i);
			if(error == -1)
				FormErrors = true;
		}
		$('#formElem').data('errors',FormErrors);
	}

	/*
	validates one fieldset
	and returns -1 if errors found, or 1 if not
	*/
	function validateStep(step){
		if(step == fieldsetCount) return;

		var error = 1;
		var hasError = false;
		$('#formElem').children(':nth-child('+ parseInt(step) +')').find(':input:not(button)').each(function(){
			var $this 		= $(this);
			var valueLength = jQuery.trim($this.val()).length;

			if(valueLength == ''){
				hasError = true;
				$this.css('background-color','#FFEDEF');
			}
			else
				$this.css('background-color','#FFFFFF');
		});
		var $link = $('#navigation li:nth-child(' + parseInt(step) + ') a');
		$link.parent().find('.error,.checked').remove();

		var valclass = 'checked';
		if(hasError){
			error = -1;
			valclass = 'error';
		}
		$('<span class="'+valclass+'"></span>').insertAfter($link);

		return error;
	}

	/*
	if there are errors don't allow the user to submit
	*/
	$('#registerButton').bind('click',function(){
		if($('#formElem').data('errors')){
			alert('Please correct the errors in the Form');
			return false;
		}
	});
});

And that’s it! I hope you liked the sliding form idea!

Message from TestkingGet professional testking ccda training to learn about critical web design tools and become expert in web design using testking mcts tutorials and testking mcitp live demos.

Manoela Ilic

Manoela is the main tinkerer at Codrops. With a background in coding and passion for all things design, she creates web experiments and keeps frontend professionals informed about the latest trends.

Stay in the loop: Get your dose of frontend twice a week

Fresh news, inspo, code demos, and UI animations—zero fluff, all quality. Make your Mondays and Thursdays creative!

Feedback 183

Comments are closed.
  1. Hey ,

    Great plugin ,i have just a small problem that i`m trying to deal with for the past 3 days,i`m trying to incorporate live validation plugin ,all works great but the validation is made per total not field by field .So i made a validation for email and username on the first field and name on the second if i enter the corect username and email it shows like is incorect only after i enter the name on the second field all other fields validate.So if i have a form with 6 field i have to get to the last one to show the user that all is completed ok.

    I know that the problem is somewhere here :
    var $this = $(this);

    var areAllValid = LiveValidation.massValidate([ email1,username,name ]);

    if(areAllValid == false ){
    …..
    Here is the website where is live in how it is now:
    http://www.gcntv.net/form/index.php

    Thanks in advance.
    Tudor

  2. Hi, Is it possible to set up some Next / Previous links ?? How do we do that with that script please ? Many thanks …

  3. Nice work! I’ll be looking for more goodies like this one in the future. Thanks for sharing.

  4. My navigation doesn’t seem to work on when I put this on my site’s registration form. What another tabbed is clicked on; any fields not filled out will turn red, but that would be the only response. What am I doing wrong? By the way, this form can be found at http://www.luvbid.com/register.php

  5. For some reason the menu is being displayed vertically at the bottom instead of horizontally…how can I fix this?

  6. Thanks for posting this, Mary Lou, it is just what I’ve been looking for!

    I hope you are still answering questions about this…

    I added several fields to one tab, but it causes the height of all the tabs to to increase. How can we make each tab height to be dynamic based on content?

  7. Having major issues implementing this form to my site…

    The sliding motion works fine, but it’s sliding to a blank screen. I have 2 tabs, and they are listed one under the other – I tab through the inputs and it slide’s both of them out the way to nothing.

    Is there anyone else having this same issue???

    I’m using the latest version of Chrome, but the same thing happens no matter what browser. However, for some unknown reason the demo on here works fine. I even downloaded the files and simply copy/pasted them into my site (so everything is identical) but AGAIN, it stuffs up, without me changing anything.

    My mind is about to blow…

    Almost on the verge of simply giving up and moving to a less fancier, easier form.

    PLEASE HELP!

  8. Added an extra password field to check if they are equal. Managed to add so all field function check is working. But did not manage to figure out how a field turns red (password). So probably I do not understand at all how jquery works and can someone be so kind to make an example.

    The following code have used in different ways to test . Its a test to check the aluminum password Length.

    function validateStep(step){
    if (step == fieldsetCount) return;
    var error = 1;
    var hasError = false;
    $(‘#formElem’).children(‘:nth-child(‘ + parseInt(step) + ‘)’).find(‘:input:not(button)’).each(function () {
    var $this = $(this);
    var invalid = ” “; // Invalid character is a space
    var minLength = 6; // Minimum length
    var pw1 = document.formElem.password.value;
    var pw2 = document.formElem.password2.value;

    var valueLength = jQuery.trim($this.val()).length;

    // Password Minimum length password
    if (document.formElem.password.value.length < minLength){
    hasError = true;
    $this.css('background-color', '#FFEDEF');

    }
    else

    $this.css('background-color', '#FFFFFF');
    });

    var $link = $('#navigation li:nth-child(' + parseInt(step) + ') a');
    $link.parent().find('.error,.checked').remove();

    var valclass = 'checked';
    if(hasError){
    error = -1;
    valclass = 'error';
    }
    $('’).insertAfter($link);

    return error;

  9. My above question does not need to be answer to me. In response to this question in this post. But there may be others who are wondering so …

    I programing in ASP so all such effects are very welcome in such an old language.

    I have written one-piece javascript that controls and check various types of fields, but also what is actually written into the fields so for an the example not injection cod, there is also other varios common things like encrypting, mail functions and so on. Infact its lots of code.

    To adda this check in to the *. js file that anyone can download the download does not feel right and it can become a security issue when making such a code known.

    Is it possible somehow to use the fancy sliding form and the include file to check the fields and still get the effect with red light fieldsbackground and of course keep all of the fancy effekts?

  10. It would be nice to show some different validations for each field using the already existing validation plugins.

  11. any idea how can i open the specific tab on page load?
    like when ajax post is done and there are some errors by php.. i can slide and focus the tab/field had the problem

  12. For those having trouble using Bill Winder’s “NEXT” button code, if copied from this page, the single quotes are not standard single quotes, but are left and right single quotes.

    Replace all single quotes with standard apostrophes (the button to the left of your ENTER key) and it will work.

  13. Hi.

    I really like this script but have some trouble with the IE. I’ve added an “empty” form for the first step, as I want no inputs on the first tab – and now the IE starts in the middle of the first tab.

    Funny thing – when I go to the the last tab and back to the first, it is shown as it should – all on the left.

    Any idea what I could change? I’ve tested to add an input field on the first tab – but the result is the same.

  14. Very sweet!!! Nicely done… also wanted to say thanks to ROBERTO SCHROEDER for adding the “optional” code.

  15. Thanks Mary Lou for this awesome codes. I like it! I decided to use this in one of the custom page that I created in my wordpress site. Now the thing is, I can’t get it to work. 🙁 Any idea how to fix this? Thanks in advance to you and to anyone who will answer me.

  16. With IE6/IE7, it’s deducting a 4px (2px either side) border in the calculation of the width of each page of the form, which means that the end total for the steps will be a few pixels short, which causes the confirmation page to show in the wrong place. To fix it, try adding a border:0; into the .step css

    17 .step{
    18 float:left;
    19 width:600px;
    20 border:0;
    21 }

    And that should do the trick. 🙂

  17. For anybody who was having problems with tabbing because the last form element in their fieldset isn’t an input tag, resulting in halfway displaying the next tab. I could not figure out how to resolve, so I disabled tabbing for all the form elements, and added a next button. Tabbing is gone, but users an still click through and this way someone doesn’t break it when trying to tab.

    To disable the tabbing, use jquery to set all form inputs tab index to -1. I lifted the next button code from someone on here.

  18. Script is great.

    Quick question – does the form post automatically based on the form action when all the fields are completed, or is some more code needed for the posting action at when the submit button is pressed?

  19. Has anyone managed to get the radio validation working. I tried the earlier code suggestion but it seems to stop the validation working.

    Also, if i press return at any point it seems to revert the form back to a blank entry and the user loses all prior inputs. Is there a way to stop that?