-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
143 lines (135 loc) · 4.43 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const day = document.getElementById("Day");
const month = document.getElementById("Month");
const year = document.getElementById("Year");
const button = document.querySelector("button");
const form = document.querySelector("form");
let yearOutput =
document.querySelector(".output").children[0].firstElementChild;
let monthOutput =
document.querySelector(".output").children[1].firstElementChild;
let dayOutput = document.querySelector(".output").children[2].firstElementChild;
let errorMsg = document.querySelectorAll(".error-msg");
let inputElements = document.querySelectorAll("input");
const inputHeadings = document.querySelectorAll(".input-headings");
let todaysDate = new Date();
let animationDuration = 50;
let messages = [
"Must be a valid day",
"Must be a valid month",
"Must be in the past",
];
let validationFunctions = {
0: (value) => (value < 1 || value > 31) && value !== "",
1: (value) => (value < 1 || value > 12) && value !== "",
2: (value) => value > todaysDate.getFullYear() && value !== "",
};
function displayError(heading, input, errorMsg, message, isValidFn) {
input.addEventListener("input", () => {
const isValid = isValidFn(input.value);
if (isValid) {
input.style.outlineColor = "hsl(0, 100%, 67%)";
heading.style.color = "hsl(0, 100%, 67%)";
errorMsg.innerText = message;
} else if (input.value == "") {
input.style.outlineColor = "hsl(0, 100%, 67%)";
heading.style.color = "hsl(0, 100%, 67%)";
errorMsg.innerText = "This field is required";
} else {
input.style.outlineColor = "hsl(0, 0%, 86%)";
heading.style.color = "hsl(0, 1%, 44%)";
errorMsg.innerText = "";
}
});
}
for (let i = 0; i < 3; i++) {
displayError(
inputHeadings[i],
inputElements[i],
errorMsg[i],
messages[i],
validationFunctions[i]
);
}
function isValidDate(year, month, day) {
let todaysDate = new Date();
let date = new Date(year, month, day);
if (
date.getFullYear() !== year ||
date.getMonth() !== month ||
date.getDate() !== day ||
date > todaysDate ||
date.getFullYear() < 0
) {
return false;
} else {
return true;
}
}
function getDifference(year, month, day) {
let birthdate = new Date(year, month, day);
const diff = todaysDate - birthdate;
const ageInYears = Math.floor(diff / (365.25 * 24 * 60 * 60 * 1000));
const ageInMonths = Math.floor(diff / (30.44 * 24 * 60 * 60 * 1000));
const ageInDays = Math.floor(diff / (24 * 60 * 60 * 1000));
const monthsRemainder = ageInMonths - ageInYears * 12;
const daysRemainder = ageInDays - ageInMonths * 30.44;
let yearAnimation = 0;
let monthAnimation = 0;
let dayAnimation = 0;
const numberOfSteps = Math.floor(animationDuration / 10); // Assuming 10 ms interval
yrInc = ageInYears / numberOfSteps;
monInc = monthsRemainder / numberOfSteps;
dayInc = daysRemainder / numberOfSteps;
let a = setInterval(() => {
yearAnimation += yrInc;
monthAnimation += monInc;
dayAnimation += dayInc;
if (
yearAnimation >= ageInYears &&
monthAnimation >= parseInt(monthsRemainder) &&
dayAnimation >= parseInt(daysRemainder)
) {
clearInterval(a);
}
yearOutput.innerText = parseInt(yearAnimation);
monthOutput.innerText = parseInt(monthAnimation);
dayOutput.innerText = parseInt(dayAnimation);
}, animationDuration);
}
form.addEventListener("submit", (event) => {
event.preventDefault();
if (
isValidDate(
parseInt(year.value),
parseInt(month.value - 1),
parseInt(day.value)
)
) {
getDifference(year.value, month.value - 1, day.value);
for (let i = 0; i < 3; i++) {
inputElements[i].style.outlineColor = "hsl(0, 0%, 86%)";
inputHeadings[i].style.color = "hsl(0, 1%, 44%)";
errorMsg[0].innerText = "";
}
} else {
let date = new Date(
inputElements[2].value,
inputElements[1].value,
inputElements[0].value
);
for (let i = 0; i < 3; i++) {
inputElements[i].style.outlineColor = "hsl(0, 100%, 67%)";
inputHeadings[i].style.color = "hsl(0, 100%, 67%)";
if (date > todaysDate) {
errorMsg[0].innerText = "Must be in the past";
} else if (inputElements[i].value == "") {
errorMsg[i].innerText = "This field is required";
}else if (inputElements[2].value < 0) {
errorMsg[0].innerText = "What are you? Jesus???";
}
else {
errorMsg[0].innerText = "Must be a valid date";
}
}
}
});