-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript.js
169 lines (142 loc) · 6.49 KB
/
javascript.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
document.addEventListener('DOMContentLoaded', function() {
// Get form and button elements
var form = document.getElementById('form');
var button = document.getElementById('submit-Btn');
// Get display elements
var yearsDisplay = document.querySelector('.display.years');
var monthsDisplay = document.querySelector('.display.months');
var daysDisplay = document.querySelector('.display.days');
// Get error message elements
var errorDay = document.querySelector('.error-text.one');
var errorMonth = document.querySelector('.error-text.two');
var errorYear = document.querySelector('.error-text.three');
// Add click event listener to the button
button.addEventListener('click', function() {
if (form.checkValidity()) {
calculation();
}else {
errorMsgs();
}
})
// Check if any field is empty
function errorMsgs() {
var inputs = form.querySelectorAll('.form-element');
inputs.forEach(function (input, index) {
var label = input.previousElementSibling; // Assuming labels are immediately preceding the input elements
if (index === 0) {
if (input.value === '') {
errorDay.textContent = 'This field is required';
input.classList.add('error');
label.classList.add('error-label');
} else if (!input.checkValidity()) {
errorDay.textContent = 'Must be a valid day';
input.classList.add('error');
label.classList.add('error-label');
} else {
errorDay.textContent = '';
input.classList.remove('error');
label.classList.remove('error-label');
}
} else if (index === 1) {
if (input.value === '') {
errorMonth.textContent = 'This field is required';
input.classList.add('error');
label.classList.add('error-label');
} else if (!input.checkValidity()) {
errorMonth.textContent = 'Must be a valid month';
input.classList.add('error');
label.classList.add('error-label');
} else {
errorMonth.textContent = ' ';
input.classList.remove('error');
label.classList.remove('error-label');
}
} else if (index === 2) {
if (input.value === '') {
errorYear.textContent = 'This field is required';
input.classList.add('error');
label.classList.add('error-label');
} else if (!input.checkValidity() || parseInt(input.value, 10) >= new Date().getFullYear()) {
errorYear.textContent = 'Must be a past year';
input.classList.add('error');
label.classList.add('error-label');
} else {
errorYear.textContent = '';
input.classList.remove('error');
label.classList.remove('error-label');
}
}
});
}
function calculation() {
// Get user input values
const userDay = parseInt(document.getElementById('day').value, 10);
const userMonth = parseInt(document.getElementById('month').value, 10);
const userYear = parseInt(document.getElementById('year').value, 10);
// Validate user input to create a valid Date object
const userDate = new Date(userYear, userMonth - 1, userDay);
// Get current date
const currentDate = new Date();
// Check if the input date is valid
if (
userDate.getDate() !== userDay ||
userDate.getMonth() + 1 !== userMonth ||
userDate.getFullYear() !== userYear
) {
// Display error message and set error-related classes
errorDay.textContent = 'Must be a valid date';
errorMonth.textContent = '';
errorYear.textContent = '';
var inputs = form.querySelectorAll('.form-element');
inputs.forEach(function (input) {
input.classList.add('error');
});
var labels = form.querySelectorAll('.form-label');
labels.forEach(function (label) {
label.classList.add('error-label');
});
// Exit the function since the input date is invalid
return;
}
// Check if the year is in the past
if (userYear >= currentDate.getFullYear()) {
// Display error message and set error-related classes
errorDay.textContent = '';
errorMonth.textContent = '';
errorYear.textContent = 'Must be a past year';
var inputs = form.querySelectorAll('.form-element');
inputs.forEach(function (input) {
input.classList.add('error');
});
var labels = form.querySelectorAll('.form-label');
labels.forEach(function (label) {
label.classList.add('error-label');
});
// Exit the function since the year is not in the past
return;
}
// Calculate the difference in milliseconds
const timeDiff = currentDate - userDate;
// Calculate years, months, and days
const years = Math.floor(timeDiff / (1000 * 60 * 60 * 24 * 365.25));
const months = Math.floor((timeDiff % (1000 * 60 * 60 * 24 * 365.25)) / (1000 * 60 * 60 * 24 * 30.44));
const days = Math.floor((timeDiff % (1000 * 60 * 60 * 24 * 30.44)) / (1000 * 60 * 60 * 24));
// Display the calculated values
yearsDisplay.textContent = years;
monthsDisplay.textContent = months;
daysDisplay.textContent = days;
// Remove error-related classes
var inputs = form.querySelectorAll('.form-element');
inputs.forEach(function (input) {
input.classList.remove('error');
});
var labels = form.querySelectorAll('.form-label');
labels.forEach(function (label) {
label.classList.remove('error-label');
});
// Reset error text content
errorDay.textContent = '';
errorMonth.textContent = '';
errorYear.textContent = '';
}
});