-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (73 loc) · 2.74 KB
/
index.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
let userForm = document.getElementById("user-form");
const retrieveEntries = () => {
let entries = localStorage.getItem("user-entries");
if (entries) {
entries = JSON.parse(entries);
} else {
entries = [];
}
return entries;
};
let userEntries = retrieveEntries();
const displayEntries = () => {
const entries = retrieveEntries();
const tableEntries = entries.map((entry) => {
const nameCell = `<td class="border px-4 py-2">${entry.name}</td>`;
const emailCell = `<td class="border px-4 py-2">${entry.email}</td>`;
const passwordCell = `<td class="border px-4 py-2">${entry.password}</td>`;
const dobCell = `<td class="border px-4 py-2">${entry.dob}</td>`;
const acceptTermsCell = `<td class="border px-4 py-2">${entry.acceptedTermsAndConditions}</td>`;
const row = `<tr>${nameCell} ${emailCell} ${passwordCell} ${dobCell} ${acceptTermsCell}</tr>`;
return row;
}).join("\n");
let details = document.getElementById("user-entries");
details.innerHTML = `<table class="min-w-full">
<tr>
<th class="px-4 py-2">Name</th>
<th class="px-4 py-2">Email</th>
<th class="px-4 py-2">Password</th>
<th class="px-4 py-2">Dob</th>
<th class="px-4 py-2">Accepted terms?</th>
</tr>
${tableEntries}
</table>`;
};
const calculateAge = (dob) => {
const today = new Date();
const birthDate = new Date(dob);
let age = today.getFullYear() - birthDate.getFullYear();
const monthDifference = today.getMonth() - birthDate.getMonth();
if (monthDifference < 0 || (monthDifference === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
};
// Validate Date of Birth (DOB) for age between 18 and 55
const isValidDOB = (dob) => {
const age = calculateAge(dob);
return age >= 18 && age <= 55;
};
const saveUserForm = (event) => {
event.preventDefault();
const name = document.getElementById("name").value;
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
const dob = document.getElementById("dob").value;
const acceptedTermsAndConditions = document.getElementById("acceptTerms").checked;
if (!isValidDOB(dob)) {
alert("Date of Birth must be for people between ages 18 and 55.");
return; // Do not proceed if DOB is invalid
}
const entry = {
name,
email,
password,
dob,
acceptedTermsAndConditions
};
userEntries.push(entry);
localStorage.setItem("user-entries", JSON.stringify(userEntries));
displayEntries();
};
userForm.addEventListener("submit", saveUserForm);
displayEntries();