Creating a Simple Form with Multiple Step and validation using JavaScript and jQuery

In this tutorial, I will show how to Create a Simple Form with Multiple Step and validation using JavaScript and jQuery. The goal of this tutorial is to give you an idea to create the said form by Manipulating the HTML Elements. Here, we will be creating a simple form that contains a single form but has multiple steps. Each step has some required fields and some have field type validation. In the form, the user cannot proceed to the next step if there's an invalid field in the visible step.

Let's get started

I will be providing the source code scripts below with a little explnation about each files do. The source code that I will provide contains an HTML scripts that uses Bootstrap v5 Framework for the user interface design. Also, I have mentioned above that this tutorial source code uses jQuery (js library). To download the Bootstrap and jQuery click the following link below.

Complie the download libraries into your source code directory. My libraries are compiled as follows:

my_sc_root_path - css - bootstrap.min.css - js - bootstrap.min.js - jquery-3.6-0.min.js

Now, let's do the coding

Creating the Interface

First, we will be creating the interface of the application that we will creating. In your preferred text editor such as Notepadd== or Sublime Text, open a new file and save the file as index.html.

The file contains the HTML Scripts for the page, forms, text inputs, and buttons. In the form, you will see multiple fieldsets and we will be hide the fields later to achieve our goal for this tutorial.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title> Simple Form with Multiple Steps</title>
  8.     <link rel="stylesheet" href="./css/bootstrap.min.css">
  9.     <link rel="stylesheet" href="./css/styles.css">
  10.     <script src="./js/bootstrap.min.js"></script>
  11.     <script src="./js/jquery-3.6.0.min.js"></script>
  12.     <script src="./js/script.js"></script>
  13.  
  14.     <style>
  15.  
  16.     </style>
  17. </head>
  18.  
  19. <body class="bg-dark bg-gradient text-light">
  20.     <main>
  21.         <div class="col-lg-12">
  22.             <h1 class="fw-bolder text-center" id="project-title">Simple Form with Multiple Steps in JavaScript</h1>
  23.         </div>
  24.         <div class="clear-fix my-5"></div>
  25.         <div class="container w-100">
  26.             <div class="col-lg-12">
  27.                 <div class="row justify-content-center">
  28.                     <div class="col-lg-7 col-md-10 col-sm-12 col-sm 12">
  29.                         <div class="card rounded-0 shadow text-dark">
  30.                             <div class="card-body">
  31.                                 <form action="" id="multiple-form" class="">
  32.                                     <fieldset>
  33.                                         <legend class="text-center">Step 1</legend>
  34.                                         <small class="text-muted"><span class="text-danger">*</span> is required field</small>
  35.                                         <div class="form-group mb-3">
  36.                                             <label for="firstname" class="control-label">Firstname <span class="text-danger">*</span></label>
  37.                                             <input type="text" name="firstname" id="firstname" class="form-control" required>
  38.                                         </div>
  39.                                         <div class="form-group mb-3">
  40.                                             <label for="middlename" class="control-label">Middle Name </label>
  41.                                             <input type="text" name="middlename" id="middlename" class="form-control">
  42.                                         </div>
  43.                                         <div class="form-group mb-3">
  44.                                             <label for="lastname" class="control-label">Last Name <span class="text-danger">*</span></label>
  45.                                             <input type="text" name="lastname" id="lastname" class="form-control" required>
  46.                                         </div>
  47.                                     </fieldset>
  48.                                     <fieldset>
  49.                                         <legend class="text-center">Step 2</legend>
  50.                                         <small class="text-muted"><span class="text-danger">*</span> is required field</small>
  51.                                         <div class="form-group mb-3">
  52.                                             <label for="email" class="control-label">Email <span class="text-danger">*</span></label>
  53.                                             <input type="email" name="email" id="email" class="form-control" required>
  54.                                         </div>
  55.                                         <div class="form-group mb-3">
  56.                                             <label for="contact" class="control-label">Contact # <span class="text-danger">*</span></label>
  57.                                             <input type="text" name="contact" id="contact" class="form-control" required>
  58.                                         </div>
  59.                                         <div class="form-group mb-3">
  60.                                             <label for="address" class="control-label">Address <span class="text-danger">*</span></label>
  61.                                             <textarea rows="3" name="address" id="address" class="form-control" required></textarea>
  62.                                         </div>
  63.                                     </fieldset>
  64.                                     <fieldset>
  65.                                         <legend class="text-center">Step 3</legend>
  66.                                         <small class="text-muted"><span class="text-danger">*</span> is required field</small>
  67.  
  68.                                         <div class="form-group mb-3">
  69.                                             <label for="other" class="control-label">Other Information <span class="text-danger">*</span></label>
  70.                                             <textarea rows="5" name="other" id="other" class="form-control" required></textarea>
  71.                                         </div>
  72.                                     </fieldset>
  73.                                     <hr>
  74.                                     <div class="mt-2 d-flex w-100 justify-content-between">
  75.                                         <div class="col-auto">
  76.                                             <button class="btn btn-sm btn-primary bg-gradient-dark rounded-0" id="prev-set" type="button">Back</button>
  77.                                         </div>
  78.                                         <div class="col-auto">
  79.                                             <button class="btn btn-sm btn-primary bg-gradient-dark rounded-0" id="next-set" type="button">Next</button>
  80.                                             <button class="btn btn-sm btn-primary bg-gradient-dark rounded-0" id="submit" form="multiple-form" type="submit">Submit</button>
  81.  
  82.                                         </div>
  83.                                     </div>
  84.                                 </form>
  85.                                 <fieldset id="data-holder">
  86.                                     <h3 class="text-center">Submitted Data</h3>
  87.                                     <div id="data">
  88.                                         <dl>
  89.                                             <dt class="text-muted">Full Name</dt>
  90.                                             <dd class="ps-3" id="fullname_txt"></dd>
  91.                                             <dt class="text-muted">Email</dt>
  92.                                             <dd class="ps-3" id="email_txt"></dd>
  93.                                             <dt class="text-muted">Contact #</dt>
  94.                                             <dd class="ps-3" id="contact_txt"></dd>
  95.                                             <dt class="text-muted">Address</dt>
  96.                                             <dd class="ps-3" id="address_txt"></dd>
  97.                                             <dt class="text-muted">Other Info.</dt>
  98.                                             <dd class="ps-3" id="other_txt"></dd>
  99.                                         </dl>
  100.                                     </div>
  101.                                     <hr>
  102.                                     <div class="text-center">
  103.                                         <button class="btn btn-default border rounded-0" onclick="location.reload()">Reset Form</button>
  104.                                     </div>
  105.                                 </fieldset>
  106.  
  107.                             </div>
  108.                         </div>
  109.                     </div>
  110.                 </div>
  111.             </div>
  112.         </div>
  113.     </main>
  114. </body>
  115.  
  116. </html>

Creating the Custom CSS

Below file is a CSS (Cascading Style Sheet) file. It contains the custom CSS Script for the design of our Interface. Save the file as styles.css. In my case, this file is being stored inside the css directory.

  1. html,
  2. body {
  3.     min-height: 100%;
  4.     width: 100%;
  5. }
  6.  
  7. main {
  8.     display: block;
  9.     min-height: 100%;
  10.     overflow: auto;
  11.     padding: 1em 0;
  12. }
  13.  
  14. main * {
  15.     font-family: Comic Sans MS;
  16. }
  17.  
  18. #project-title {
  19.     text-shadow: 3px 3px 7px #000;
  20.     padding: 2.5em 1em !important;
  21. }
  22.  
  23. #multiple-form fieldset {
  24.     width: 100%;
  25.     transition: all .2s ease-in-out
  26. }
  27.  
  28. #multiple-form fieldset:not(fieldset:nth-child(1)) {
  29.     display: none;
  30. }
  31.  
  32. #prev-step,
  33. #submit,
  34. #data-holder {
  35.     display: none;
  36. }

Creating the JS File

Lastly, this file is the final code file. It is a JS (JavaScript) file that contains the important script for this tutorial. It contains the function and scripts that manipulates our HTML Elements to acheive our Goal. This contains the buttons and form submission event listeners. Save this file as script.js and my case, this file is stored inside the js directory.

  1. // Initiate the script below on page window is succesffuly loaded
  2. $(function() {
  3.  
  4.     // Varialbles
  5.     var current = 0;
  6.     var prev = $('#prev-set')
  7.     var next = $('#next-set')
  8.     var form = $('#multiple-form')
  9.     var fs = $('form#multiple-form fieldset')
  10.     var fsCount = fs.length
  11.  
  12.     // Button Manipulation when changing Steps
  13.     function vissible_change() {
  14.         if ($('form#multiple-form fieldset:visible').index() == 0) {
  15.             prev.hide()
  16.             $('#submit').hide()
  17.         }
  18.         if ($('form#multiple-form fieldset:visible').index() > 0 && $('form#multiple-form fieldset:visible').index() <= (fsCount - 1)) {
  19.             prev.show()
  20.             next.show()
  21.             $('#submit').hide()
  22.         }
  23.  
  24.         if ($('form#multiple-form fieldset:visible').index() == (fsCount - 1)) {
  25.             next.hide()
  26.             $('#submit').show()
  27.         }
  28.     }
  29.  
  30.  
  31.     // Next Step Click Event Listener
  32.     next.click(function() {
  33.         var currentField = fs[current]
  34.         var is_valid = true;
  35.         $('form#multiple-form fieldset:visible [required]').each(function() {
  36.             if (!$(this).is(':valid')) {
  37.                 $(this).addClass('border-danger').focus()
  38.                 console.log($(this)[0].reportValidity())
  39.                 is_valid = false;
  40.                 return false;
  41.             }
  42.         })
  43.         if (!is_valid)
  44.             return false;
  45.         if (current < (fsCount - 1)) {
  46.             current++;
  47.             if (!!fs[current]) {
  48.                 $(currentField).hide()
  49.                 $(fs[current]).show()
  50.                 vissible_change()
  51.                 $(fs[current]).find('input,textarea,select').first().focus()
  52.             }
  53.         }
  54.     })
  55.  
  56.     // Previous Step Click Event Listener
  57.     prev.click(function() {
  58.         var currentField = fs[current]
  59.         if (current > -1) {
  60.             current--;
  61.             if (!!fs[current]) {
  62.                 $(currentField).hide()
  63.                 $(fs[current]).show()
  64.                 vissible_change()
  65.             }
  66.         }
  67.     })
  68.  
  69.     // DOM CSS Manipulation for Inputs that became valid from invalid
  70.     $('form#multiple-form fieldset [required]').on('input', function() {
  71.         $(this).removeClass('border-danger')
  72.     })
  73.  
  74.     // Form Submit Event Listener
  75.     form.submit(function(e) {
  76.         e.preventDefault()
  77.         $('#fullname_txt').text($('#lastname').val() + ', ' + $('#firstname').val() + ' ' + $('#middlename').val())
  78.         $('#email_txt').text($('#email').val())
  79.         $('#contactl_txt').text($('#contact').val())
  80.         $('#address_txt').text($('#address').val())
  81.         $('#other_txt').text($('#other').val())
  82.         form.hide()
  83.         $('#data-holder').show()
  84.     })
  85. })

That's it! You can now test the source code on your end and see if it works as we planned.

Sample Result Snapshots

Step 1

This step display only the Next Button because this is only the first step of the form

Step 2

This step display both the Back and Next buttons to allow user navigate from the previous step or to proceed to the next step

Field Validation

The application will return a validation message when user click the Next Button that has an Invalid field values on the current step.

Step 3

This step displays the Submit Button because it is the last step of the form.

Success Submission

This page displays the submitted data.

If you encountered an error on your end, feel free to leave a comment below. You can also download the working source code that I provided for this tutorial. The download button is located below this article.

That's the end of this tutorial. I hope you'll find tutorial useful for your future Web Application Projects. Explore more on this website for more Free Source Codes and Tutorials.

Enjoy Coding :)

Add new comment