Skip to content
Tutorials Center
My learning

Topics

Lesson 50 of 56 ยท Projects

Project: Multi-Step Form Wizard

Build a multi-step form wizard in jQuery with per-step validation, an animated progress bar, focus management and a summary screen that prints the answers.

Long forms scare people away. Splitting one into short steps (contact, plan, preferences) makes each screen feel small, lets you validate as you go and gives users a sense of progress. That is the idea behind a wizard. It is also a great final project for the Forms chapter, because it needs everything at once: reading values, validation with accessible errors, fade animations, an animated progress bar and careful handling of the submit event.

You will build it in two steps. First the navigation and progress bar on their own, so you can see how the steps are shown and hidden. Then the complete wizard: a rule table for validation, error messages tied to fields with ARIA, and a summary screen that shows the collected answers on the page. Nothing is posted to a server; the final "submit" prints what would be sent.

Anatomy of the wizard

Steps One fieldset per step, only one visible.
Progress A bar and a label say where you are.
Gate Next works only when the step is valid.
Summary The last screen shows every answer.
1 / 2

One form, many fieldsets

Keep the whole wizard inside one <form>. Each step is a <fieldset> with a <legend>, which gives screen readers the grouping for free. All fields remain in the DOM even when their step is hidden, which is what makes the final .serializeArray() see every answer, and what lets "Back" show the values the user typed earlier. If you replaced the steps with dynamically generated content, you would need to store the answers yourself.

Showing one step is a matter of hiding the others. The helper below fades out the current fieldset, then fades in the next one inside the completion callback, so the two never overlap. It also updates the progress bar with .animate() on the width. The .stop(true) call matters: if the user clicks quickly, the older animation is discarded instead of queued, as explained in the animation queue lesson.

Step navigation and an animated progress bar

The last line of goTo() is easy to overlook. Hiding the step that has focus drops focus back to the page, so the next keystroke starts from the top of the document. Moving focus to the new legend (with tabindex="-1", so it is focusable by script but not part of the Tab order) keeps keyboard and screen reader users where they were.

Validation as a table of rules

The simplest way to validate a wizard is a table of rules, one function per field name, each returning true or an error message. The || operator makes the rule short: test || "message" is true when the test passes and the message otherwise.

JavaScript
var rules = {
  name:  function (v) { return v.trim().length >= 2 || "Enter your name (at least 2 characters)."; },
  email: function (v) { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v) || "Enter a valid email address."; },
  plan:  function (v) { return !!v || "Choose a plan."; },
  terms: function (v) { return v === true || "You must accept the terms."; }
};

The wizard then asks, for each field on the current step, "what is the value, and what does the rule say?". Reading a value depends on the control. A text field gives .val(), a checkbox gives its checked property and a radio group gives the value of the :checked radio, or an empty string when none is chosen (see working with form controls).

When a rule fails, three things happen. The message goes into an element next to the field; the field gets aria-invalid="true"; and aria-describedby points to the message so a screen reader reads it with the field. After validating all fields, focus moves to the first invalid one. Errors are cleared as soon as the user edits the field, so the screen is never scolding after the problem is fixed.

Pressing Enter in a text field submits the form, and the wizard turns that to its advantage: the primary button is a real submit button, and the submit handler decides what "submit" means on the current step. Calling event.preventDefault() stops the browser from posting anywhere, which is required in this sandbox anyway.

The complete wizard

The final step does not post the data. It reads everything with .serializeArray(), which returns {name, value} pairs for each successful control, and prints a summary. Values are inserted with .text(), so nothing the user typed can become markup. Checkboxes sharing a name (the extras) arrive as several pairs, which the code joins with commas.

The complete wizard with validation and summary

Walk through the logic once more, because the order of operations is where wizards go wrong.

  1. submit is the single entry point. The Next button, the Enter key and the Finish button all produce a submit event, so the validation rule lives in one place.
  2. validateStep() checks only the visible step. The other steps' fields may be empty, and that is fine until the user gets there. If you validated the whole form on each click, step 1 would complain about step 3.
  3. A seen object avoids duplicates. A radio group or a checkbox group has several elements with one name, and the rule should run once for the group.
  4. Fields without a rule are skipped. topic and extras have no entry in rules, so they are always accepted.
  5. Going back never validates. Users can always revisit an earlier step; they are only blocked when going forward.

Because the form has novalidate, the browser's built-in bubbles are switched off and our messages take over. For simple wizards you can also combine the two: keep the HTML required and type="email" attributes for browsers without JavaScript, and rely on this script when it runs. The form validation lesson shows how to call checkValidity() yourself.

Ideas to extend it

  • Conditional steps. Skip the plan step when a returning customer is detected, by computing the next index with a small function instead of current + 1.
  • A review link per answer. Each line of the summary could include a button that calls goTo() with the index of the step that owns the answer.
  • Real submission. Replace showSummary() with $.ajax({ method: "POST", data: $form.serialize() }) and show the summary in the .done() callback, once the server confirms (the AJAX patterns lesson covers loading and error states).
  • Saving a draft. In a normal page you could store the form in localStorage on every change, so a reload does not lose progress. The sandbox here blocks storage, so this stays an exercise.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does the wizard move focus to the new step's legend after the fade-in?

    When the element that has focus is hidden, focus is lost. Moving it to the new step (with tabindex=-1 on the legend) keeps assistive technology users oriented.
  2. 2. What does validateStep() check, and why?

    Validating the current step gives immediate, relevant feedback. Fields on later steps are checked when the user reaches them.
  3. 3. How does the submit handler stop the browser from posting the form?

    preventDefault() cancels the browser's default action for submit, so the page does not navigate. novalidate only turns off the built-in validation bubbles.
  4. 4. Why are the summary values inserted with .text() instead of .html()?

    User input must never be interpreted as HTML. .text() sets textContent, which escapes everything automatically.
  5. 5. What does the expression v.trim().length >= 2 || "message" return when the value is valid?

    The comparison produces true, and || stops there, so the message is only returned when the test is false. The caller checks for exactly true.

Summary

  • Put all steps in one form as fieldsets with legends; hide all but one and keep every field in the DOM so answers survive navigation.
  • Fade the old step out and the new one in inside the callback, animate the progress bar with .stop(true).animate() and move focus to the new step.
  • Validate only the current step with a table of rules; show messages next to the field and tie them in with aria-invalid and aria-describedby.
  • Make the primary button a real submit button and always call preventDefault(); the submit handler is the single place that decides between "next" and "finish".
  • Collect the final answers with .serializeArray() and render them with .text().

You have now built all the course projects. To test yourself, continue with Practice Exercises with Solutions.