-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathscript.js
205 lines (177 loc) · 5.94 KB
/
script.js
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
const stepInfo = document.getElementById("stepInfo");
const navLeftGroup = document.getElementById("navLeft");
const navRightGroup = document.getElementById("navRight");
const nameInput = document.getElementById("name");
const idNumInput = document.getElementById("idNum");
const emailInput = document.getElementById("email");
const birthdateInput = document.getElementById("birthdate");
const documentInput = document.getElementById("document");
const departmentInput = document.getElementById("department");
const skillsInput = document.getElementById("skills");
const termsCheckbox = document.getElementById("terms");
const nameVal = document.getElementById("name-val");
const idVal = document.getElementById("id-val");
const emailVal = document.getElementById("email-val");
const bdVal = document.getElementById("bd-val");
const cvVal = document.getElementById("cv-val");
const deptVal = document.getElementById("dept-val");
const skillsVal = document.getElementById("skills-val");
const form = document.getElementById("myForm");
const formStepsID = ["one", "two", "three", "four"];
let currentFormStep = 0;
const editButtons = {
"name-edit": 0,
"id-edit": 0,
"email-edit": 0,
"bd-edit": 0,
"cv-edit": 1,
"dept-edit": 1,
"skills-edit": 2,
};
const updateSummaryValues = () => {
nameVal.textContent = nameInput.value;
idVal.textContent = idNumInput.value;
emailVal.textContent = emailInput.value;
bdVal.textContent = birthdateInput.value;
const fileName = documentInput.files[0]?.name;
if (fileName) {
const extension = fileName.split(".").pop();
const baseName = fileName.split(".")[0];
const truncatedName =
baseName.length > 10 ? baseName.substring(0, 10) + "..." : baseName;
cvVal.textContent = `${truncatedName}.${extension}`;
} else {
cvVal.textContent = "No file selected";
}
deptVal.textContent = departmentInput.value;
skillsVal.textContent = skillsInput.value || "No skills submitted";
};
const updateStepVisibility = () => {
formStepsID.forEach((step) => {
document.getElementById(step).style.display = "none";
});
document.getElementById(formStepsID[currentFormStep]).style.display = "block";
stepInfo.textContent = `Step ${currentFormStep + 1} of ${formStepsID.length}`;
if (currentFormStep === 3) {
updateSummaryValues();
}
navLeftGroup.style.display = currentFormStep === 0 ? "none" : "block";
navRightGroup.style.display =
currentFormStep === formStepsID.length - 1 ? "none" : "block";
const currentStep = document.getElementById(formStepsID[currentFormStep]);
const firstInput = currentStep.querySelector("input, select, textarea");
if (firstInput) {
firstInput.focus();
}
};
const showError = (input, message) => {
const formControl = input.parentElement;
const errorSpan = formControl.querySelector(".error-message");
input.classList.add("error");
input.setAttribute("aria-invalid", "true");
input.setAttribute("aria-describedby", errorSpan.id);
errorSpan.textContent = message;
};
const clearError = (input) => {
const formControl = input.parentElement;
const errorSpan = formControl.querySelector(".error-message");
input.classList.remove("error");
input.removeAttribute("aria-invalid");
input.removeAttribute("aria-describedby");
errorSpan.textContent = "";
};
const validateStep = (currentStep) => {
let isValid = true;
if (currentStep === 0) {
if (nameInput.value.trim() === "") {
showError(nameInput, "Name is required");
isValid = false;
}
if (idNumInput.value.trim() === "") {
showError(idNumInput, "ID number is required");
isValid = false;
}
if (emailInput.value.trim() === "" || !emailInput.validity.valid) {
showError(emailInput, "A valid email is required");
isValid = false;
}
if (birthdateInput.value === "") {
showError(birthdateInput, "Date of birth is required");
isValid = false;
}
} else if (currentStep === 1) {
if (!documentInput.files[0]) {
showError(documentInput, "CV/Resume is required");
isValid = false;
}
if (departmentInput.value === "") {
showError(departmentInput, "Department selection is required");
isValid = false;
}
} else if (currentStep === 2) {
if (!termsCheckbox.checked) {
showError(termsCheckbox, "Terms and conditions must be accepted");
isValid = false;
}
}
return isValid;
};
const realtimeValidation = () => {
nameInput.addEventListener("input", () => {
if (nameInput.value.trim() !== "") clearError(nameInput);
});
idNumInput.addEventListener("input", () => {
if (idNumInput.value.trim() !== "") clearError(idNumInput);
});
emailInput.addEventListener("input", () => {
if (emailInput.value.trim() !== "") clearError(emailInput);
});
birthdateInput.addEventListener("change", () => {
if (birthdateInput.value !== "") clearError(birthdateInput);
});
documentInput.addEventListener("change", () => {
if (documentInput.files[0]) clearError(documentInput);
});
departmentInput.addEventListener("change", () => {
if (departmentInput.value !== "") clearError(departmentInput);
});
termsCheckbox.addEventListener("change", () => {
if (termsCheckbox.checked) clearError(termsCheckbox);
});
};
document.addEventListener("DOMContentLoaded", () => {
navLeftGroup.style.display = "none";
updateStepVisibility();
realtimeValidation();
navRightGroup.addEventListener("click", () => {
if (currentFormStep < formStepsID.length - 1) {
if (validateStep(currentFormStep)) {
currentFormStep++;
updateStepVisibility();
}
}
});
navLeftGroup.addEventListener("click", () => {
if (currentFormStep > 0) {
currentFormStep--;
updateStepVisibility();
}
});
Object.keys(editButtons).forEach((buttonId) => {
const button = document.getElementById(buttonId);
button.addEventListener("click", (e) => {
e.preventDefault();
currentFormStep = editButtons[buttonId];
updateStepVisibility();
});
});
});
form.addEventListener("submit", (e) => {
e.preventDefault();
if (validateStep(2)) {
alert("Form submitted successfully!");
form.reset();
currentFormStep = 0;
updateStepVisibility();
}
});