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:

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

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
8 + 4 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.