-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
238 lines (205 loc) · 8.07 KB
/
script.js
File metadata and controls
238 lines (205 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* Multi-Step Form Wizard - Main JavaScript
* Handles form navigation, validation, and submission
*
* @author Molla Samser
* @email help@rskworld.in
* @website https://rskworld.in
* @contact +91 9330539277
*
* For website or application development, contact us today!
*/
document.addEventListener('DOMContentLoaded', function() {
// DOM Elements
const form = document.getElementById('multiStepForm');
const steps = document.querySelectorAll('.step');
const prevBtn = document.getElementById('prevBtn');
const nextBtn = document.getElementById('nextBtn');
const submitBtn = document.getElementById('submitBtn');
const progressBar = document.querySelector('.progress-bar');
let currentStep = 0;
const totalSteps = steps.length - 1; // Excluding the thank you step
// Initialize the form
function initForm() {
showStep(currentStep);
updateButtons();
}
// Show current step and hide others
function showStep(stepIndex) {
// Hide all steps
steps.forEach((step, index) => {
step.classList.remove('active', 'slide-in-left', 'slide-in-right');
if (index === stepIndex) {
step.classList.add('active');
}
});
// Update progress bar
const progress = (stepIndex / totalSteps) * 100;
progressBar.style.width = `${progress}%`;
progressBar.setAttribute('aria-valuenow', progress);
// Update form title if needed
document.title = `Step ${stepIndex + 1} of ${totalSteps} - Multi-Step Form`;
}
// Update navigation buttons state
function updateButtons() {
// Previous button
if (currentStep === 0) {
prevBtn.disabled = true;
} else {
prevBtn.disabled = false;
}
// Next/Submit button
if (currentStep === totalSteps - 1) {
nextBtn.style.display = 'none';
submitBtn.style.display = 'block';
} else if (currentStep === totalSteps) {
// On the thank you page
prevBtn.style.display = 'none';
nextBtn.style.display = 'none';
submitBtn.style.display = 'none';
} else {
nextBtn.style.display = 'block';
submitBtn.style.display = 'none';
}
}
// Validate current step
function validateStep(stepIndex) {
const currentStepFields = steps[stepIndex].querySelectorAll('[required]');
let isValid = true;
currentStepFields.forEach(field => {
if (!field.checkValidity()) {
field.classList.add('is-invalid');
isValid = false;
} else {
field.classList.remove('is-invalid');
field.classList.add('is-valid');
}
});
return isValid;
}
// Go to next step
function nextStep() {
if (validateStep(currentStep)) {
// Add slide-out animation
steps[currentStep].classList.add('slide-out-left');
// Move to next step after animation
setTimeout(() => {
currentStep++;
showStep(currentStep);
steps[currentStep].classList.add('slide-in-right');
updateButtons();
// Scroll to top of form
form.scrollIntoView({ behavior: 'smooth', block: 'start' });
}, 300);
}
}
// Go to previous step
function prevStep() {
// Add slide-out animation
steps[currentStep].classList.add('slide-out-right');
// Move to previous step after animation
setTimeout(() => {
currentStep--;
showStep(currentStep);
steps[currentStep].classList.add('slide-in-left');
updateButtons();
// Scroll to top of form
form.scrollIntoView({ behavior: 'smooth', block: 'start' });
}, 300);
}
// Handle form submission
function handleSubmit(e) {
e.preventDefault();
if (validateStep(currentStep)) {
// Show loading state
submitBtn.disabled = true;
submitBtn.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Submitting...';
// Simulate form submission
setTimeout(() => {
// In a real app, you would submit the form data to a server here
const formData = new FormData(form);
const formValues = {};
// Convert FormData to object
formData.forEach((value, key) => {
formValues[key] = value;
});
// Move to thank you step
currentStep++;
showStep(currentStep);
updateButtons();
// Display submitted data
displaySubmittedData(formValues);
// Reset form and buttons
form.reset();
submitBtn.disabled = false;
submitBtn.innerHTML = 'Submit';
// Scroll to top of form
form.scrollIntoView({ behavior: 'smooth', block: 'start' });
}, 1500);
}
}
// Display submitted data on the thank you page
function displaySubmittedData(data) {
const formDataContainer = document.getElementById('formData');
formDataContainer.innerHTML = '<h5>Your Information:</h5>';
// Map field IDs to display names
const fieldLabels = {
'fullName': 'Full Name',
'email': 'Email',
'phone': 'Phone',
'address': 'Address',
'city': 'City',
'zip': 'ZIP Code',
'referral': 'How you heard about us',
'comments': 'Comments'
};
// Create and append data paragraphs
for (const [key, value] of Object.entries(data)) {
if (value && fieldLabels[key]) {
const p = document.createElement('p');
p.innerHTML = `<strong>${fieldLabels[key]}:</strong> ${value}`;
formDataContainer.appendChild(p);
}
}
// Add timestamp
const timestamp = new Date().toLocaleString();
const p = document.createElement('p');
p.innerHTML = `<strong>Submitted on:</strong> ${timestamp}`;
p.classList.add('text-muted', 'small');
formDataContainer.appendChild(p);
}
// Event Listeners
nextBtn.addEventListener('click', nextStep);
prevBtn.addEventListener('click', prevStep);
form.addEventListener('submit', handleSubmit);
// Keyboard navigation
document.addEventListener('keydown', function(e) {
if (e.key === 'Enter' && currentStep < totalSteps - 1) {
e.preventDefault();
nextStep();
} else if (e.key === 'ArrowLeft' && currentStep > 0) {
e.preventDefault();
prevStep();
} else if (e.key === 'ArrowRight' && currentStep < totalSteps - 1) {
e.preventDefault();
nextStep();
}
});
// Real-time validation for fields
form.querySelectorAll('input, select, textarea').forEach(field => {
field.addEventListener('input', function() {
if (field.checkValidity()) {
field.classList.remove('is-invalid');
field.classList.add('is-valid');
} else {
field.classList.remove('is-valid');
}
});
});
// Initialize the form
initForm();
// Add animation classes
document.querySelectorAll('.step').forEach(step => {
step.style.animation = 'fadeIn 0.5s ease-in-out';
});
});