-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
174 lines (148 loc) · 5.57 KB
/
app.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
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
const inputContainer = document.querySelector(".input-container");
const Button = document.querySelector(".calculate-btn");
Button.addEventListener("click", calculateAge);
// create error message
function createText(text) {
const errormsg = document.createElement("p");
errormsg.classList.add("error");
errormsg.textContent = `${text}`;
return errormsg;
}
// age calculation
function calculateAge() {
const dayInput = document.querySelector("#day-input").value,
monthInput = document.querySelector("#month-input").value,
yearInput = document.querySelector("#year-input").value;
(dayOutput = document.querySelector(".dayOutput")),
(monthOutput = document.querySelector(".monthOutput")),
(yearOutput = document.querySelector(".yearOutput"));
if (dayInput === "" || monthInput === "" || yearInput === "") {
const labels = document.getElementsByTagName("label");
inputContainer.classList.add("error");
// console.log(labels.classList)
// console.log(inputContainer.classList)
Array.from(labels).forEach((label) => {
const errormsg = createText("This field is required");
label.appendChild(errormsg);
setTimeout(function () {
inputContainer.classList.remove("error");
label.removeChild(errormsg);
}, 2000);
});
}
if (monthInput > 12) {
const labels = document.getElementsByTagName("label");
inputContainer.classList.add("error");
const errormsg = createText("This must be a valid month");
labels[1].appendChild(errormsg);
setTimeout(() => {
inputContainer.classList.remove("error");
labels[1].removeChild(errormsg);
}, 2000);
}
const currentDate = new Date(),
currentYear = currentDate.getFullYear(),
currentMonth = currentDate.getMonth() + 1,
currentDay = currentDate.getDate();
const lastDayOfCurrentMonth = new Date(
currentYear,
currentMonth - 1,
0
).getDate();
const lastDayOfUserInputMonth = new Date(
currentYear,
currentMonth,
0
).getDate();
if (dayInput > lastDayOfUserInputMonth) {
const labels = document.getElementsByTagName("label");
inputContainer.classList.add("error");
const errormsg = createText("This must be a valid day");
labels[0].appendChild(errormsg);
setTimeout(function () {
inputContainer.classList.remove("error");
labels[0].removeChild(errormsg);
}, 2000);
}
if (yearInput > currentYear) {
const labels = document.getElementsByTagName("label");
inputContainer.classList.add("error");
const errormsg = createText("Must be in the past");
labels[2].appendChild(errormsg);
setTimeout(function () {
inputContainer.classList.remove("error");
labels[2].removeChild(errormsg);
}, 2000);
}
let ageDay = currentDay - dayInput,
ageMonth = currentMonth - monthInput,
ageYear = currentYear - yearInput;
if (ageDay < 0) {
ageMonth--;
ageDay = ageDay + lastDayOfCurrentMonth;
}
if (ageMonth < 0) {
ageYear--;
ageMonth = ageMonth + 12;
}
if (
dayInput !== "" &&
dayInput <= lastDayOfUserInputMonth &&
monthInput !== "" &&
monthInput >= 1 &&
monthInput <= 12 &&
yearInput !== "" &&
yearInput <= currentYear
) {
dayOutput.innerHTML = ageDay;
monthOutput.innerHTML = ageMonth;
yearOutput.innerHTML = ageYear;
}
}
// const button = document.querySelector(".arrowIcon");
// let errorMessage = document.querySelector(".errormsg");
// Button.addEventListener("click", getResult);
// function getResult() {
// const dayInput = document.querySelector("#day-input");
// const monthInput = document.querySelector("#month-input");
// const yearInput = document.querySelector("#year-input");
// if (
// dayInput.value === "" &&
// monthInput.value === "" &&
// yearInput.value === ""
// ) {
// document.getElementById("dayerrormsg").style.display = "block";
// document.getElementById("montherrormsg").style.display = "block";
// document.getElementById("yearerrormsg").style.display = "block";
// } else if (dayInput.value === "" || dayInput.value === isNaN) {
// document.getElementById("dayerrormsg").style.display = "block";
// document.getElementById("montherrormsg").style.display = "none";
// document.getElementById("yearerrormsg").style.display = "none";
// console.log(alert("close page"));
// } else if (monthInput.value === "" || monthInput.value === isNaN) {
// document.getElementById("dayerrormsg").style.display = "none";
// document.getElementById("montherrormsg").style.display = "block";
// document.getElementById("yearerrormsg").style.display = "none";
// } else if (yearInput.value === "" || monthInput.value === isNaN) {
// document.getElementById("dayerrormsg").style.display = "none";
// document.getElementById("montherrormsg").style.display = "none";
// document.getElementById("yearerrormsg").style.display = "block";
// } else {
// document.getElementById("dayerrormsg").style.display = "none";
// document.getElementById("montherrormsg").style.display = "none";
// document.getElementById("yearerrormsg").style.display = "none";
// }
// const age = currentyear - `${yearInput.value}`;
// let currentdate = new Date();
// if (dayInput.value === "" || dayInput.value === isNaN) {
// e.target.textContent = "This field is required";
// e.target.style.color = "red";
// } else {
// e.target.textContent = "";
// }
// if (monthInput.value === "") {
// errorMessage.textContent = "This field is required";
// } else {
// errorMessage.textContent = "This field is required";
// }
// }