-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
150 lines (134 loc) · 4.7 KB
/
Copy pathscript.js
File metadata and controls
150 lines (134 loc) · 4.7 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
document.addEventListener("DOMContentLoaded", () => {
const form = document.getElementById("registrationForm");
const entriesTableBody = document.getElementById("entriesTableBody");
const storageKey = "user-entries";
// Display entries from Local Storage
const displayEntries = () => {
const entries = JSON.parse(localStorage.getItem(storageKey)) || [];
entriesTableBody.innerHTML = "";
if (entries.length === 0) {
const row = document.createElement("tr");
row.innerHTML = `<td colspan="5" class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 text-center">No entries yet.</td>`;
entriesTableBody.appendChild(row);
return;
}
entries.forEach((entry) => {
const row = document.createElement("tr");
const escapeHTML = (str) =>
str.replace(
/[&<>"']/g,
(tag) =>
({
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
}[tag])
);
row.innerHTML = `
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">${escapeHTML(
entry.name
)}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">${escapeHTML(
entry.email
)}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">${escapeHTML(
entry.password
)}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">${escapeHTML(
entry.dob
)}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">${
entry.terms ? "Yes" : "No"
}</td>
`;
entriesTableBody.appendChild(row);
});
};
// Form validation
const validateForm = () => {
let isValid = true;
document
.querySelectorAll(".error-message")
.forEach((el) => (el.textContent = ""));
const name = document.getElementById("name").value.trim();
const email = document.getElementById("email").value.trim();
const password = document.getElementById("password").value.trim();
const dob = document.getElementById("dob").value;
const terms = document.getElementById("terms").checked;
if (!name) {
document.getElementById("nameError").textContent =
"Please fill in this field.";
isValid = false;
}
if (!email) {
document.getElementById("emailError").textContent =
"Please fill in this field.";
isValid = false;
} else if (!email.includes("@")) {
document.getElementById(
"emailError"
).textContent = `Please include an '@' in the email address. '${email}' is missing an '@'.`;
isValid = false;
}
if (!password) {
document.getElementById("passwordError").textContent =
"Please fill in this field.";
isValid = false;
}
if (!dob) {
document.getElementById("dobError").textContent =
"Please fill in this field.";
isValid = false;
} else {
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--;
}
if (age < 18) {
document.getElementById(
"dobError"
).textContent = `You must be at least 18 years old.`;
isValid = false;
} else if (age > 55) {
document.getElementById(
"dobError"
).textContent = `You must be no older than 55 years old.`;
isValid = false;
}
}
if (!terms) {
document.getElementById("termsError").textContent =
"Please tick this box if you want to proceed.";
isValid = false;
}
return isValid;
};
// Form submit
form.addEventListener("submit", (event) => {
event.preventDefault();
if (validateForm()) {
const formData = {
name: document.getElementById("name").value.trim(),
email: document.getElementById("email").value.trim(),
password: document.getElementById("password").value.trim(),
dob: document.getElementById("dob").value,
terms: document.getElementById("terms").checked,
};
const entries = JSON.parse(localStorage.getItem(storageKey)) || [];
entries.push(formData);
localStorage.setItem(storageKey, JSON.stringify(entries));
displayEntries();
form.reset();
}
});
// Initial load
displayEntries();
});