Multi Step Form
Home
Snippets
Multi Step Form
HTML
CSS
JS
<div id="form-container"> <div id="steps-bar"> <div class="step-indicator active">1</div> <div class="step-indicator">2</div> <div class="step-indicator">3</div> </div> <form id="multiStepForm"> <div class="step active"> <h3>Personal Information</h3> <label for="firstName">First Name:</label> <input type="text" id="firstName" name="firstName" required><br><br> <label for="lastName">Last Name:</label> <input type="text" id="lastName" name="lastName" required><br><br> </div> <div class="step"> <h3>Contact Information</h3> <label for="email">Email:</label> <input type="email" id="email" name="email" required><br><br> <label for="phone">Phone Number:</label> <input type="tel" id="phone" name="phone" required><br><br> </div> <div class="step"> <h3>Account Details</h3> <label for="username">Username:</label> <input type="text" id="username" name="username" required><br><br> <label for="password">Password:</label> <input type="password" id="password" name="password" required><br><br> </div> <div class="buttons"> <button type="button" id="prevBtn" onclick="prevStep()" disabled>Previous</button> <button type="button" id="nextBtn" onclick="nextStep()">Next</button> <button type="submit" id="submitBtn" style="display:none;">Submit</button> </div> <span id="result"></span> </form> </div>
*{ font-family: Arial, sans-serif; box-sizing: border-box; } body { background-color: #333333; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; } #form-container { background-color: #ffffff; padding: 20px 40px; border-radius: 8px; box-shadow: 0 0 10px #cdf5664c; width: 100%; max-width: 400px; } #steps-bar { display: flex; justify-content: space-between; margin-bottom: 20px; position: relative; } #steps-bar::before { content: ''; position: absolute; top: 50%; left: 0; right: 0; height: 2px; background-color: #dddddd; z-index: 0; } .step-indicator { width: 30px; height: 30px; border-radius: 50%; background-color: #dddddd; color: #ffffff; text-align: center; line-height: 30px; font-weight: bold; z-index: 1; position: relative; transition: background-color 0.3s, transform 0.3s; } .step-indicator.active { background-color: #333333; } .step-indicator.completed { background-color: orangered; } .step { display: none; } .step.active { display: block; } .step input{ width: 100%; height: 40px; border-radius: 5px; border: 2px solid orangered; margin-top: 5px; } .buttons { display: flex; justify-content: space-between; margin-top: 5px; } .buttons button { padding: 10px 20px; border: none; border-radius: 5px; background-color: orangered; color: white; cursor: pointer; transition: background-color 0.3s; } .buttons button[disabled] { background-color: #fe9d7a; cursor: not-allowed; } .buttons button:hover:not([disabled]) { background-color: #333333; }
const steps = document.querySelectorAll('.step'); const stepIndicators = document.querySelectorAll('.step-indicator'); let currentStep = 0; function showStep(step) { stepIndicators.forEach((indicator, index) => { indicator.classList.toggle('active', index === step); indicator.classList.toggle('completed', index < step); }); steps.forEach((stepElement, index) => { stepElement.classList.toggle('active', index === step); }); document.getElementById('prevBtn').disabled = (step === 0); if (step === steps.length - 1) { document.getElementById('nextBtn').style.display = 'none'; document.getElementById('submitBtn').style.display = 'inline-block'; } else { document.getElementById('nextBtn').style.display = 'inline-block'; document.getElementById('submitBtn').style.display = 'none'; } } function nextStep() { if (currentStep < steps.length - 1) { currentStep++; showStep(currentStep); } } function prevStep() { if (currentStep > 0) { currentStep--; showStep(currentStep); } } document.getElementById('multiStepForm').addEventListener('submit', function (event) { event.preventDefault(); document.getElementById("result").innerText = "Form submitted!"; }); showStep(currentStep);
Ad #1
Ad #2
Scroll to Top