Create a Multi Step Form in Bootstrap 5 & JavaScript Last Updated : 29 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Forms are the gateway for interaction between users and web services. As the complexity of data collection increases, so does the need for more user-friendly form interfaces. A multi-step form, by breaking down the data collection process into manageable chunks, enhances user experience and improves form completion rates. We will going to create a multi-step form in Bootstrap5 and JavaScript.ApproachHTML Structure: Begin with a well-defined HTML structure for the form, including a form container, input fields, and navigation buttons. Styling with CSS and Bootstrap: Utilize Bootstrap's classes for styling, ensuring responsiveness and user-friendliness. Bootstrap utilities like margin, padding, and flexbox aid in designing layouts and transitions between form steps.Adding a Progress Bar: Enhance the form with a progress bar, offering users a visual representation of their completion status. This not only motivates users but also provides clarity on their progress through the form.Implementing Interactivity with JavaScript: JavaScript adds interactivity to the form, enabling logic for step transitions and dynamic display of form sections.Example: This example shows the implementation of the above-explained approach. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Multi Step Form</title> <!-- Bootstrap CSS --> <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> <!-- Google Font --> <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600&display=swap" rel="stylesheet"> <!-- Custom CSS --> <style> body { font-family: 'Open Sans', sans-serif; background-color: #f8f9fa; } .container { max-width: 500px; background-color: #ffffff; margin: 40px auto; padding: 40px; border-radius: 12px; box-shadow: 0px 6px 18px rgba(0, 0, 0, 0.1); } .step { display: none; } .active { display: block; } input { padding: 15px 20px; width: 100%; font-size: 1em; border: 1px solid #e3e3e3; border-radius: 5px; } input:focus { border: 1px solid #009688; outline: 0; } .invalid { border: 1px solid #ffaba5; } #nextBtn, #prevBtn { background-color: #009688; color: #ffffff; border: none; padding: 13px 30px; font-size: 1em; cursor: pointer; border-radius: 5px; flex: 1; margin-top: 5px; transition: background-color 0.3s ease; } #prevBtn { background-color: #ffffff; color: #009688; border: 1px solid #009688; } #prevBtn:hover, #nextBtn:hover { background-color: #00796b; color: #ffffff; } .progress { margin-bottom: 20px; } </style> </head> <body> <div class="container"> <div class="progress"> <div class="progress-bar progress-bar-striped bg-success" role="progressbar" style="width: 0%" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div> </div> <div class="step active"> <p class="text-center mb-4">Step 1 Content</p> <div class="mb-3"> <input type="text" placeholder="Field 1" oninput="this.className = ''" name="field1"> </div> </div> <div class="step"> <p class="text-center mb-4">Step 2 Content</p> <div class="mb-3"> <input type="text" placeholder="Field 2" oninput="this.className = ''" name="field2"> </div> </div> <div class="step"> <p class="text-center mb-4">Step 3 Content</p> <div class="mb-3"> <input type="text" placeholder="Field 3" oninput="this.className = ''" name="field3"> </div> </div> <div class="form-footer d-flex"> <button type="button" id="prevBtn" onclick="nextPrev(-1)">Previous</button> <button type="button" id="nextBtn" onclick="nextPrev(1)">Next</button> </div> </div> <script> let currentTab = 0; showTab(currentTab); function showTab(n) { let x = document.getElementsByClassName("step"); x[n].style.display = "block"; let progress = (n / (x.length - 1)) * 100; document.querySelector(".progress-bar").style.width = progress + "%"; document.querySelector(".progress-bar").setAttribute("aria-valuenow", progress); document.getElementById("prevBtn").style.display = n == 0 ? "none" : "inline"; document.getElementById("nextBtn").innerHTML = n == x.length - 1 ? "Submit" : "Next"; } function nextPrev(n) { let x = document.getElementsByClassName("step"); if (n == 1 && !validateForm()) return false; x[currentTab].style.display = "none"; currentTab += n; if (currentTab >= x.length) { resetForm(); return false; } showTab(currentTab); } function validateForm() { let valid = true; let x = document.getElementsByClassName("step"); let y = x[currentTab].getElementsByTagName("input"); for (var i = 0; i < y.length; i++) { if (y[i].value == "") { y[i].className += " invalid"; valid = false; } } return valid; } function resetForm() { let x = document.getElementsByClassName("step"); for (var i = 0; i < x.length; i++) { x[i].style.display = "none"; } let inputs = document.querySelectorAll("input"); inputs.forEach(input => { input.value = ""; input.className = ""; }); currentTab = 0; showTab(currentTab); document.querySelector(".progress-bar").style.width = "0%"; document.querySelector(".progress-bar").setAttribute("aria-valuenow", 0); document.getElementById("prevBtn").style.display = "none"; } </script> </body> </html> Output: Create a Multi Step Form in Bootstrap 5 & JavaScript Comment More infoAdvertise with us Next Article How to create a Multi Step Progress Bar in HTML CSS and JavaScript? H haneesr8pj Follow Improve Article Tags : Project JavaScript Web Technologies Dev Scripter JavaScript-Projects Dev Scripter 2024 +2 More Similar Reads Create a To Do List using Bootstrap 5 A To-Do List is a tool for organizing tasks, allowing users to list, prioritize, and manage their activities, ensuring efficiency and productivity in completing them. Here we will create a ToDo list using Bootstrap. We will create our layout or component using Bootstrap predefined utilities and comp 3 min read How to create a Multi Step Progress Bar in HTML CSS and JavaScript? In this article, we will create a multi-step progress bar. A Multi-Step Progress Bar is a user interface element created with HTML, CSS, and JavaScript. It guides users through a series of steps or stages, visually displaying their progress and allowing step-by-step navigation in a multi-step proces 3 min read How to Create a Form with Multiple Steps using CSS and JavaScript ? A multi-step form divides a long form into smaller, more manageable sections or steps. This makes it easier for users to fill out and makes the form less intimidating. We will explore the approach to building a multi-step form using CSS and JavaScript. ApproachCreate a form container. Inside the for 3 min read How to Create a Bootstrap Form Validation ? A Bootstrap Form Validation ensures that user input complies with established standards before submission, improves the user experience, and minimizes server-side processing for incorrect inputs. We can add client-side validation to their forms by using two different methods including Bootstrap buil 3 min read How to create notes taking site using HTML, Bootstrap and JavaScript ? We are going to make a website that will take our notes and saves them for our future use using HTML, CSS and JavaScript .Prerequisite:Basic understanding of HTML, Bootstrap, and JavaScript.Approach:HTML: We will create the basic framework of the website using HTML.Bootstrap: makes our work easier a 2 min read Bootstrap 5 Dropdowns Via JavaScript Bootstrap 5 Dropdowns can be triggered in two ways: via data attributes and JavaScript. When using JavaScript, you need to define a function that utilizes the predefined toggle() or show() method to control the dropdown from a button or link. Additionally, even with JavaScript, you must include the 4 min read Like