The following code is a step-by-step form in which the user will fill out part of a form, click on the next button, and then fill out another part of the form until the last section in which the form will be submitted. I have included some <inputs> both required and not required to show you that the user cannot proceed until the section is completed based on required input:
HTML
HTML Code:
<form>
<fieldset>
<legend>Contact Information</legend>
<section>
<label for="txtName">* Full Name</label>
<input id="txtName" required="required" type="text" />
<label for="txtEmail">* Email Address</label>
<input id="txtEmail" required="required" type="email" />
<label for="txtPhone">* Phone Number</label>
<input id="txtPhone" required="required" type="tel" />
</section>
</fieldset>
<fieldset>
<legend>Questions, Comments, or Concerns</legend>
<section>
<label for="cboType">Inquery Type</label>
<select>
<option>Question</option>
<option>Comment</option>
<option>Concern</option>
</select>
<label for="txtDescription">Description</label>
<textarea id="txtDescription"></textarea>
</section>
</fieldset>
<footer>
<input id="btnLeft" type="button" value="< Back" />
<input id="btnRight" type="button" value="Next >" />
</footer>
</form>
CSS
Code:
form fieldset {
border-width: 0;
margin: 0;
padding: 0;
}
form fieldset legend {
border-bottom: 3px dotted rgb(102, 102, 102);
font-size: 1.5em;
margin: 0;
padding: 0;
width: 100%;
}
form fieldset section label {
display: block;
padding-top: 1em;
}
form fieldset section input, form fieldset section select, form fieldset section textarea {
box-sizing: border-box; /*select width fix */
margin: 0 2%;
padding: .5em 1%;
width: 94%;
}
form footer {
padding-top: 1em;
}
form footer input[type="button"] {
background: rgb(202, 60, 60);
border-radius: 4px;
color: rgb(255, 255, 255);
cursor: pointer;
margin: 0 2%;
padding: 1em;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
}
form footer input[type="button"]:last-child {
background: rgb(28, 184, 65);
float: right;
}
form footer input[type="button"]:disabled {
background: rgb(102, 102, 102);
color: rgb(224, 224, 224);
cursor: not-allowed;
}
JQuery
Code:
$(function() {
//Hide all but the first element
$('form fieldset:not(:first)').hide();
//Disable the back button to begin with
$('#btnLeft').prop('disabled', true);
$('#btnLeft').on('click', function() {
//get the index of the previous fieldset
var leftIndex = $('form fieldset:visible').index() - 1;
changeFieldset(leftIndex);
});
$('#btnRight').on('click', function() {
//get all invalid input
var invalid = $('form fieldset:visible').find(':invalid');
//get the index of the next fieldset
var rightIndex = $('form fieldset:visible').index() + 1;
if(invalid.length > 0) {
//inform the user that there are invalid inputs by changing the border-color
invalid.css('border-color', 'rgb(202, 60, 60)');
} else if(rightIndex == $('form fieldset').length) {
$('form').submit();
} else {
//Set all the border-colors back to normal
$('form fieldset:visible').find('input').css('border-color', 'rgb(255, 255, 255)');
changeFieldset(rightIndex);
}
});
$('form').on('submit', function() {
alert('submitted!');
});
function changeFieldset(index) {
//hide the visible fieldset
$('form fieldset:visible').hide();
//show the fieldset at the index;
$('form fieldset').eq(index).show();
//disable the back button(if applicable)
$('#btnLeft').prop('disabled', index === 0);
}
});
Fiddle: http://codepen.io/anon/pen/ZWGwdb