-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (52 loc) · 1.9 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
window.onload = function() {
const today = new Date().toDateString();
if (localStorage.getItem('date') !== today) {
localStorage.clear();
localStorage.setItem('date', today);
}
};
function isValidAge(dob) {
const today = new Date();
const birthDate = new Date(dob);
let age = today.getFullYear() - birthDate.getFullYear();
const monthDiff = today.getMonth() - birthDate.getMonth();
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age >= 18 && age <= 55;
}
const form = document.getElementById('registration-form');
const entriesTableBody = document.getElementById('entries-table-body');
form.addEventListener('submit', function(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 termsAccepted = document.getElementById('terms').checked;
if (!isValidAge(dob)) {
alert("Date of birth must be between ages 18 and 55.");
return;
}
const entry = { name, email, password, dob, termsAccepted };
const entries = JSON.parse(localStorage.getItem('entries')) || [];
entries.push(entry);
localStorage.setItem('entries', JSON.stringify(entries));
addEntryToTable(entry);
});
function addEntryToTable(entry) {
const row = document.createElement('tr');
row.innerHTML = `
<td>${entry.name}</td>
<td>${entry.email}</td>
<td>${entry.password}</td>
<td>${entry.dob}</td>
<td>${entry.termsAccepted ? 'true' : 'false'}</td>
`;
entriesTableBody.appendChild(row);
}
function loadEntries() {
const entries = JSON.parse(localStorage.getItem('entries')) || [];
entries.forEach(addEntryToTable);
}
loadEntries();