-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
124 lines (96 loc) · 3.27 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
let form = document.getElementById("form");
// localStorage.clear();
// let Entries = [];
const retriveEntries = () => {
let entries = localStorage.getItem("userEntry");
if (entries) {
entries = JSON.parse(entries);
} else {
entries = [];
}
return entries;
};
let Entries = retriveEntries();
const displayEntries = () => {
const entries = retriveEntries();
const rows = entries
.map((entry) => {
const name = `<td class="td">${entry.name}</td>`;
const email = `<td class="td">${entry.email}</td>`;
const password = `<td class="td">${entry.password}</td>`;
const dob = `<td class="td">${entry.dob}</td>`;
const accseptConditions = `<td class="td">${entry.accseptConditions}</td>`;
const row = `<tr>${name} ${email} ${password} ${dob} ${accseptConditions}</tr>`;
return row;
})
.join("\n");
let tableDiv = document.getElementById("tableDiv");
// <th class="th">Name</th> inside oneMore head for name
tableDiv.innerHTML = `<table class="table" border="2">
<tr>
<th class="th">Name</th>
<th class="th">Email</th>
<th class="th">Password</th>
<th class="th">Dob</th>
<th class="th">Accepted terms?</th>
</tr>
${rows}
</table>`;
};
// const saveUserFrom = () => {
const saveUserFrom = (event) => {
event.preventDefault();
let name = document.getElementById("name").value;
let email = document.getElementById("email").value;
let password = document.getElementById("password").value;
let dob = document.getElementById("dob").value;
let accseptConditions = document.getElementById("agree").checked;
let entry_obj = {
name,
email,
password,
dob,
accseptConditions,
};
Entries.push(entry_obj);
localStorage.setItem("userEntry", JSON.stringify(Entries));
displayEntries();
};
form.addEventListener("submit", saveUserFrom);
displayEntries();
// Add additional validations to the date input field so that it accepts date of birth for people between ages 18 and 55 only. You'll need to figure out how to do this.
function getAge(today, birthDate) {
// var today = new Date();
// var birthDate = new Date(DOB);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
let dateELE = document.getElementById("dob");
dateELE.addEventListener("change", () => {
let [year, month, date] = document.getElementById("dob").value.split("-");
let dob = new Date(year, month, date);
let Today = new Date();
age = getAge(Today, dob);
dateELE.style.border = "2px solid rgba(0, 0, 0, 0.4)";
if (age < 18 || age > 55) {
dateELE.setCustomValidity("Your age is not lies between 18 and 55");
dateELE.style.border = "2px solid red";
return;
} else {
dateELE.setCustomValidity("");
}
});
const email = document.getElementById("email");
email.addEventListener("input", () => validate(email));
function validate(ele) {
if (ele.validity.typeMismatch) {
ele.setCustomValidity("The Email is not in the right format!!!");
ele.reportValidity();
} else {
ele.setCustomValidity("");
}
}