Multi step Form with Client-side Validation jQuery Plugin
File Structure
form-validation/
┣ js/
┃ ┗ main.js
┣ plugins/
┃ ┣ jquery.maskedinput.js
┃ ┗ jquery.validate.js
┗ index.php
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery accordion form with validation</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col">
<div id="wrap">
<div id="main">
<h3>This form is complete - in only 3 steps!</h3>
<form name="cmaForm" id="cmaForm" method="post">
<input type="hidden" name="recordRequestPrimaryServiceID" id="recordRequestPrimaryServiceID" value="100">
<input type="hidden" name="recordClientServices" id="recordClientServices" value="1,3">
<ul id="stepForm" class="ui-accordion-container">
<li id="sf1">
<a href='#' class="ui-accordion-link"></a>
<div>
<fieldset>
<legend>Step 1 of 3</legend>
<div class="requiredNotice">*Required Field</div>
<h5 class="stepHeader">Tell us about the property you're buying</h5>
<label for="recordPurchaseMetRealtor" class="input required">Are you currently working with a
<br>real estate agent?</label> No:
<input name="recordPurchaseMetRealtor" type="radio" checked="checked" class="inputclass" value="0"> Yes:
<input name="recordPurchaseMetRealtor" type="radio" class="inputclass pageRequired" value="1" title="Please choose Yes or No">
<div class="formspacer"></div>
<label for="recordPurchaseTimeFrameID" class="input required">When would you like to move?</label>
<select name="recordPurchaseTimeFrameID" id="recordPurchaseTimeFrameID" class="inputclass pageRequired" title="Select a Time Frame">
<option value="">-Select-</option>
<option value="1">Less than 3 months</option>
<option value="2">3-6 months</option>
<option value="3">6-9 months</option>
<option value="4">9-12 months</option>
<option value="5">Over 12 months</option>
</select>
<br>
<label for="recordPurchasePriceRangeID" class="input required">Purchase price range:</label>
<select name="recordPurchasePriceRangeID" id="recordPurchasePriceRangeID" class="inputclass pageRequired" title="Select a Price Range">
<option value="">-Select-</option>
<option value="1"></option>
<option value="2">$75,000 - $100,000</option>
<option value="3">$100,000 - $125,000</option>
</select>
<br>
<label for="recordPurchaseState" class="input required">State:</label>
<select name="recordPurchaseState" id="recordPurchaseState" class="inputclass pageRequired" title="Select a State">
<option value="">-Select-</option>
<option value="AL">Alabama</option>
</select>
<br>
<label for="recordPurchasePropertyTypeID" class="input">Desired property type:</label>
<select name="recordPurchasePropertyTypeID" id="recordPurchasePropertyTypeID" class="inputclass" title="Select a Property Type">
<option value="">-Select-</option>
<option value="1">Single Family Detached</option>
</select>
<br>
<div class="buttonWrapper">
<input name="formNext1" type="button" class="open1 nextbutton" value="Next" alt="Next" title="Next">
</div>
</fieldset>
</div>
</li>
<li id="sf2">
<a href='#' class="ui-accordion-link">
</a>
<div>
<fieldset>
<legend>Step 2 of 3</legend>
<div class="requiredNotice">*Required Field</div>
<h3 class="stepHeader">Tell us about the property you're selling</h3>
<label for="recordClientTimeFrameID" class="input required">When would you like to sell?</label>
<select name="recordClientTimeFrameID" id="recordClientTimeFrameID" class="inputclass pageRequired" title="Select a Time Frame">
<option value="">-Select-</option>
<option value="1">Less than 3 months</option>
</select>
<br>
<label for="recordClientHomeTypeID" class="input required">Type of property you are selling:</label>
<select name="recordClientHomeTypeID" id="recordClientHomeTypeID" class="inputclass pageRequired" title="Select a Property Type">
<option value="">-Select-</option>
<option value="1">Single Family Detached</option>
</select>
<br>
<label for="recordPropertyAddress1" class="input required">Property Street Address:</label>
<input name="recordPropertyAddress1" id="recordPropertyAddress1" class="inputclass pageRequired" title="Street Address is required" maxlength="254" onblur="recordClientAddress1.value = this.value">
<br>
<label for="recordPropertyAddress2" class="input">Address (2):</label>
<input name="recordPropertyAddress2" id="recordPropertyAddress2" class="inputclass" maxlength="254" onblur="recordClientAddress2.value = this.value">
<br>
<label for="recordPropertyCity" class="input required">City:</label>
<input name="recordPropertyCity" id="recordPropertyCity" class="inputclass pageRequired" title="City is required" maxlength="254" onblur="recordClientCity.value = this.value">
<br>
<label for="recordPropertyState" class="input required">State:</label>
<select name="recordPropertyState" id="recordPropertyState" class="inputclass pageRequired" title="Select a State" onchange="recordClientState.value = this.value">
<option value="">-Select-</option>
<option value="AL">Alabama</option>
</select>
<br>
<label for="recordPropertyZip" class="input required">Zip:</label>
<input name="recordPropertyZip" id="recordPropertyZip" class="inputclass pageRequired" title="Zip Code is required" maxlength="254" onblur="recordClientZip.value = this.value">
<br>
<label for="recordClientPropertyValueID" class="input required">Estimated Market Value:</label>
<select name="recordClientPropertyValueID" id="recordClientPropertyValueID" class="inputclass pageRequired" title="Select a Price Range">
<option value="">-Select-</option>
<option value="1">Less Than $75K</option>
<option value="2">$75-$100K</option>
</select>
<br>
<label for="recordPropertyBedroomsID" class="input">Bedrooms:</label>
<select name="recordPropertyBedroomsID" id="recordPropertyBedroomsID" class="inputclass">
<option value="">-Select-</option>
<option value="1">1</option>
</select>
<br>
<label for="recordPropertyBathroomsId" class="input">Bathrooms:</label>
<select name="recordPropertyBathroomsId" id="recordPropertyBathroomsId" class="inputclass">
<option value="">-Select-</option>
<option value="1">1</option>
</select>
<br>
<label for="recordPropertyAgeId" class="input">Approx. Age of Home:</label>
<select name="recordPropertyAgeId" id="recordPropertyAgeId" class="inputclass">
<option value="">-Select-</option>
<option value="1">Less Than 1 year</option>
<option value="2">1-5 years</option>
</select>
<br>
<label for="recordPropertySqFt" class="input">Approx. Square Footage:</label>
<input name="recordPropertySqFt" id="recordPropertySqFt" class="inputclass" maxlength="254">
<br>
<div class="buttonWrapper">
<input name="formBack0" type="button" class="open0 prevbutton" value="Back" alt="Back" title="Back">
<input name="formNext2" type="button" class="open2 nextbutton" value="Next" alt="Next" title="Next">
</div>
</fieldset>
</div>
</li>
<li id="sf3">
<a href='#' class="ui-accordion-link">
</a>
<div>
<fieldset>
<legend>Step 3 of 3</legend>
<div class="requiredNotice">*Required Field</div>
<h3 class="stepHeader">Tell us about yourself</h3>
<label for="recordClientNameFirst" class="input required">First Name:</label>
<input name="recordClientNameFirst" id="recordClientNameFirst" class="inputclass pageRequired" title="First Name is required" maxlength="254">
<br>
<label for="recordClientNameLast" class="input required">Last Name:</label>
<input name="recordClientNameLast" id="recordClientNameLast" class="inputclass pageRequired" maxlength="254" title="Last Name is required">
<br>
<label for="recordClientAddress1" class="input required">Current Address:</label>
<input name="recordClientAddress1" id="recordClientAddress1" class="inputclass pageRequired" maxlength="254" title="Address is required">
<br>
<label for="recordClientAddress2" class="input">Address (2):</label>
<input name="recordClientAddress2" id="recordClientAddress2" class="inputclass" maxlength="254">
<br>
<label for="recordClientCity" class="input required">City:</label>
<input name="recordClientCity" id="recordClientCity" class="inputclass pageRequired" maxlength="254" title="City is required">
<br>
<label for="recordClientState" class="input required">State:</label>
<select name="recordClientState" id="recordClientState" class="inputclass pageRequired" title="Select a State">
<option value="">-Select-</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
</select>
<br>
<label for="recordClientZip" class="input required">Zip:</label>
<input name="recordClientZip" id="recordClientZip" class="inputclass pageRequired" maxlength="12" title="Zip Code is required">
<br>
<label for="recordClientPhone" class="input required">Phone Number:</label>
<input name="recordClientPhone" id="recordClientPhone" class="inputclass pageRequired" maxlength="254" title="Phone Number is required">
<br>
<label for="recordClientPhoneAlt" class="input">Alternate Number:</label>
<input name="recordClientPhoneAlt" id="recordClientPhoneAlt" class="inputclass" maxlength="254">
<br>
<label for="recordClientEmail" class="input required">Email Address:</label>
<input name="recordClientEmail" id="recordClientEmail" class="inputclass pageRequired email" maxlength="254" title="Email address is required">
<br>
<label for="recordClientEmail1" class="input required">Confirm Email:</label>
<input name="recordClientEmail1" id="recordClientEmail1" class="inputclass pageRequired" equalTo: "'#recordClientEmail" maxlength="254" title="Please confirm your email address">
<br>
<br>
<p class="formDisclaimer">This is a sample form, no information is sent anywhere.</p>
<div class="buttonWrapper">
<input name="formBack1" type="button" class="open1 prevbutton" value="Back" alt="Back" title="Back">
<input name="submit" type="submit" id="submit" value="Submit" class="submitbutton" alt="Submit" title="Submit">
</div>
</fieldset>
</div>
</li>
</ul>
</form>
</div>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="plugins/jquery.validate.js"></script>
<script src="plugins/jquery.maskedinput.js"></script>
<script src="https://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>
<script src="js/main.js"></script>
</body>
</html>
main.js
$(document).ready(function() {
$("#recordClientPhone").mask("(999) 999-9999");
$("#recordClientPhoneAlt").mask("(999) 999-9999");
$("#recordClientZip").mask("99999");
$("#recordPropertyZip").mask("99999");
$("#recordPurchaseZip").mask("99999");
// add * to required field labels
$('label.required').append(' <strong>*</strong> ');
// accordion functions
var accordion = $("#stepForm").accordion();
var current = 0;
$.validator.addMethod("pageRequired", function(value, element) {
var $element = $(element)
function match(index) {
return current == index && $(element).parents("#sf" + (index + 1)).length;
}
if (match(0) || match(1) || match(2)) {
return !this.optional(element);
}
return "dependency-mismatch";
}, $.validator.messages.required)
var v = $("#cmaForm").validate({
errorClass: "warning",
onkeyup: false,
onfocusout: false,
submitHandler: function() {
alert("Submitted, thanks!");
}
});
// back buttons do not need to run validation
$("#sf2 .prevbutton").click(function() {
accordion.accordion("option", "active", 0);
current = 0;
});
$("#sf3 .prevbutton").click(function() {
accordion.accordion("option", "active", 1);
current = 1;
});
// these buttons all run the validation, overridden by specific targets above
$(".open2").click(function() {
if (v.form()) {
accordion.accordion("option", "active", 2);
current = 2;
}
});
$(".open1").click(function() {
if (v.form()) {
accordion.accordion("option", "active", 1);
current = 1;
}
});
$(".open0").click(function() {
if (v.form()) {
accordion.accordion("option", "active", 0);
current = 0;
}
});
});
You can download plugin files, below is button to download source code