-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.js
More file actions
401 lines (354 loc) · 11.8 KB
/
validation.js
File metadata and controls
401 lines (354 loc) · 11.8 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
let currentRow = null;
const data = {
India: {
states: {
Delhi: ["New Delhi", "Old Delhi"],
Kolkata: ["Kolkata", "Howrah"],
Mumbai: ["Mumbai", "Thane"],
},
},
USA: {
states: {
California: ["Los Angeles", "San Francisco"],
Texas: ["Houston", "Austin"],
"New York": ["New York City", "Buffalo"],
},
},
UK: {
states: {
England: ["London", "Manchester"],
Scotland: ["Edinburgh", "Glasgow"],
},
},
};
//
function updateStates() {
const country = document.getElementById("country").value;
const stateSelect = document.getElementById("state");
const citySelect = document.getElementById("city");
// Clear previous state and city options
stateSelect.innerHTML = '<option value="">Select State</option>';
citySelect.innerHTML = '<option value="">Select City</option>';
if (country && data[country]) {
const states = data[country].states;
// Populate states based on selected country
for (const state in states) {
const option = document.createElement("option");
option.value = state;
option.textContent = state;
stateSelect.appendChild(option);
}
}
}
function updateCities() {
const country = document.getElementById("country").value;
const state = document.getElementById("state").value;
const citySelect = document.getElementById("city");
// Clear previous city options
citySelect.innerHTML = '<option value="">Select City</option>';
if (country && state && data[country] && data[country].states[state]) {
const cities = data[country].states[state];
// Populate cities based on selected state
cities.forEach((city) => {
const option = document.createElement("option");
option.value = city;
option.textContent = city;
citySelect.appendChild(option);
});
}
}
function saveData(data) {
let storedData = JSON.parse(localStorage.getItem("formData")) || [];
storedData.push(data);
localStorage.setItem("formData", JSON.stringify(storedData));
}
function loadSavedData() {
const storedData = JSON.parse(localStorage.getItem("formData")) || [];
const tableBody = document.querySelector("#data-table tbody");
tableBody.innerHTML = "";
storedData.forEach((data, index) => {
const row = document.createElement("tr");
row.innerHTML = `
<td>${data.fullname}</td>
<td>${data.phone}</td>
<td>${data.email}</td>
<td>${data.gender}</td>
<td>${data.country}</td>
<td class="action-buttons">
<button class="edit-btn" onclick="editRow(${index})">Edit</button>
<button class="delete-btn" onclick="deleteRow(${index})">Delete</button>
</td>
`;
tableBody.appendChild(row);
});
}
function submitForm() {
const form = document.getElementById("registrationForm");
if (!validateForm()) {
return;
}
const formData = {
fullname: form.fullname.value,
phone: form.phone.value,
email: form.email.value,
age: form.age.value,
dob: form.dob.value,
gender: form.gender.value,
experience: form.experience.value,
preferredMedium: form.preferredMedium.value,
preferredDate: form.preferredDate.value,
address: form.address.value,
country: form.country.value,
state: form.state.value,
city: form.city.value,
};
let storedData = JSON.parse(localStorage.getItem("formData")) || [];
const existingData = storedData.find(
(data) => data.email === formData.email || data.phone === formData.phone
);
if (existingData && currentRow === null) {
showNotification("The data with this email or phone already exists.");
return;
}
if (document.getElementById("submitBtn").textContent === "Update") {
storedData[currentRow] = formData;
showNotification("Form updated successfully");
currentRow = null;
} else {
if (!existingData) {
storedData.push(formData);
showNotification("Form submitted successfully");
}
}
localStorage.setItem("formData", JSON.stringify(storedData));
resetForm();
loadSavedData();
}
// Modify editRow to set currentRow and button text
function editRow(index) {
document.getElementById("submitBtn").textContent = "Update";
const storedData = JSON.parse(localStorage.getItem("formData")) || [];
const data = storedData[index];
document.getElementById("fullname").value = data.fullname;
document.getElementById("phone").value = data.phone;
document.getElementById("email").value = data.email;
document.getElementById("address").value = data.address;
document.getElementById("age").value = data.age;
document.getElementById("country").value = data.country;
updateStates();
document.getElementById("state").value = data.state;
updateCities();
document.getElementById("city").value = data.city;
// const preferredMedium = data.preferredMedium;
// if (preferredMedium) {
// document.querySelector(
// `input[name="preferredMedium"][value="${preferredMedium}"]`
// ).checked = true;
// }
const experience = data.experience;
if (experience) {
document.querySelector(
`input[name="experience"][value="${experience}"]`
).checked = true;
}
const gender = data.gender;
if (gender) {
document.querySelector(
`input[name="gender"][value="${gender}"]`
).checked = true;
}
currentRow = index;
}
let deleteIndex;
function deleteRow(index) {
deleteIndex = index;
document.getElementById("confirmDeleteModal").style.display = "block";
}
function confirmDelete() {
const userInput = document.getElementById("deleteConfirmationInput").value;
if (userInput.toLowerCase() === "delete") {
let storedData = JSON.parse(localStorage.getItem("formData")) || [];
storedData.splice(deleteIndex, 1);
localStorage.setItem("formData", JSON.stringify(storedData));
loadSavedData();
showNotification("Data deleted successfully");
} else {
alert('Action canceled. You did not type "delete".');
}
closeModal();
}
function cancelDelete() {
closeModal();
}
function closeModal() {
document.getElementById("confirmDeleteModal").style.display = "none";
document.getElementById("deleteConfirmationInput").value = "";
}
function showNotification(message) {
const notification = document.getElementById("notification");
notification.textContent = message;
notification.style.display = "block";
setTimeout(() => {
notification.style.display = "none";
}, 3000);
}
function resetForm() {
document.getElementById("registrationForm").reset();
document.getElementById("submitBtn").innerText = "Submit";
}
document.getElementById("fullname").addEventListener("blur", () => {
const fullname = document.getElementById("fullname").value;
const nameErr = document.getElementById("fullname_error");
if (fullname === "" || /\d/.test(fullname)) {
nameErr.textContent = "Please enter your name properly.";
return false;
}
nameErr.textContent = "";
return true;
});
document.getElementById("phone").addEventListener("input", () => {
const phone = document.getElementById("phone").value;
const phoneErr = document.getElementById("phone_error");
const phonePattern = /^\d{10}$/;
if (phone === "" || !phone.match(phonePattern)) {
phoneErr.textContent = "Please enter a valid 10-digit number.";
return false;
}
phoneErr.textContent = "";
return true;
});
document.getElementById("email").addEventListener("input", () => {
const email = document.getElementById("email").value;
const emailErr = document.getElementById("email_error");
if (email === "" || !email.includes("@") || !email.includes(".")) {
emailErr.textContent = "Please enter a valid email address.";
return false;
}
emailErr.textContent = "";
return true;
});
document.getElementById("age").addEventListener("input", () => {
const age = document.getElementById("age").value;
const ageErr = document.getElementById("age_error");
if (age === "" || age < 1 || age > 120) {
ageErr.textContent = "Please enter a valid age.";
return false;
}
ageErr.textContent = "";
return true;
});
// document.getElementById('address').addEventListener('input', () => {
// const address = document.getElementById("address").value;
// const addrErr = document.getElementById("address_error");
// if (address === "") {
// addrErr.textContent = "Please fill this required field.";
// return false;
// }
// addrErr.textContent = "";
// return true;
// });
document.getElementById("country").addEventListener("change", () => {
const country = document.getElementById("country").value;
const countryErr = document.getElementById("country_error");
if (country === "") {
countryErr.textContent = "Please fill this required field.";
return false;
}
countryErr.textContent = "";
});
document.getElementById("state").addEventListener("change", () => {
const state = document.getElementById("state").value;
const stateErr = document.getElementById("state_error");
if (state === "") {
stateErr.textContent = "Please fill this required field.";
return false;
}
stateErr.textContent = "";
});
document.getElementById("city").addEventListener("change", () => {
const city = document.getElementById("city").value;
const cityErr = document.getElementById("city_error");
if (city === "") {
cityErr.textContent = "Please fill this required field.";
}
});
function validateForm() {
let isValid = true;
// Validate Fullname
const fullname = document.getElementById("fullname").value;
const fullnameErr = document.getElementById("fullname_error");
if (fullname === "" || /\d/.test(fullname)) {
fullnameErr.textContent = "Please enter a valid name.";
isValid = false;
} else {
fullnameErr.textContent = "";
}
// Validate Phone
const phone = document.getElementById("phone").value;
const phoneErr = document.getElementById("phone_error");
const phonePattern = /^\d{10}$/;
if (phone === "" || !phone.match(phonePattern)) {
phoneErr.textContent = "Please enter a valid 10-digit phone number.";
isValid = false;
} else {
phoneErr.textContent = "";
}
// Validate Email
const email = document.getElementById("email").value;
const emailErr = document.getElementById("email_error");
if (email === "" || !email.includes("@") || !email.includes(".")) {
emailErr.textContent = "Please enter a valid email address.";
isValid = false;
} else {
emailErr.textContent = "";
}
// Validate Age
const age = document.getElementById("age").value;
const ageErr = document.getElementById("age_error");
if (age === "" || age < 1 || age > 120) {
ageErr.textContent = "Please enter a valid age.";
isValid = false;
} else {
ageErr.textContent = "";
}
// Validate Address
// const address = document.getElementById("address").value;
// const addrErr = document.getElementById("address_error");
// if (address === "") {
// addrErr.textContent = "Please fill this required field.";
// isValid = false;
// } else {
// addrErr.textContent = "";
// }
// Validate Country
const country = document.getElementById("country").value;
const countryErr = document.getElementById("country_error");
if (country === "") {
countryErr.textContent = "Please select a country.";
isValid = false;
} else {
countryErr.textContent = "";
}
// Validate State
const state = document.getElementById("state").value;
const stateErr = document.getElementById("state_error");
if (state === "") {
stateErr.textContent = "Please select a state.";
isValid = false;
} else {
stateErr.textContent = "";
}
// Validate City
const city = document.getElementById("city").value;
const cityErr = document.getElementById("city_error");
if (city === "") {
cityErr.textContent = "Please select a city.";
isValid = false;
} else {
cityErr.textContent = "";
}
return isValid;
}
window.onload = function () {
loadSavedData();
};