A multi-step form wizard
Validate each step before moving on. Show progress and keep Back available.
jQuery · A multi-step form wizardAdvanced
let step = 0;
const $steps = $('.step');
function show(n) {
step = n;
$steps.hide().eq(n).show();
$('#progress').text('Step ' + (n + 1) + ' of ' + $steps.length);
$('#back').toggle(n > 0);
$('#next').text(n === $steps.length - 1 ? 'Finish' : 'Next');
}
$('#next').on('click', function () {
const valid = $steps.eq(step).find('[required]').filter(function () { return !this.value.trim(); }).length === 0;
if (!valid) return $('#msg').text('Please fill in this step.');
$('#msg').text('');
if (step === $steps.length - 1) return $('#wizard').trigger('submit');
show(step + 1);
});
$('#back').on('click', function () { show(step - 1); });
show(0);wizardstepsformvalidation