-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
140 lines (115 loc) · 3.98 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
const DayInput = document.querySelector(`#day`)
const MonthInput = document.querySelector(`#month`)
const YearInput = document.querySelector(`#year`)
// const ErrorMsg = document.querySelector(`.error`) *//
const calc_button = document.querySelector(`#calc_btn`)
const AgeOutput= document.querySelector(`.age-edit`)
const MonthOutput = document.querySelector(`.month-edit`)
const DayOutput = document.querySelector(`.day-edit`)
const error_day = document.querySelector(`.error-day`);
const error_month = document.querySelector(`.error-month`);
const error_year = document.querySelector(`.error-year`);
// Validation for day
let valDay = () => {
let day = +DayInput.value
valid = false
if(day > 31){
error_day.textContent = `invalid date`
} else if(day = 0){
error_day.textContent = `pls input a valid no.`
}else if(!DayInput.value){
error_day.textContent = `field is required`;
}else{
error_day.textContent = ``;
valid = true
}
return valid;
}
DayInput.addEventListener(`input`, valDay);
// validation for month
let valMonth = () => {
let month = +MonthInput.value
valid = false
if(month > 12){
error_month.textContent = `invalid date`
} else if(month = 0){
error_month.textContent = `pls input a valid no.`
}else if(!MonthInput.value){
error_month.textContent = `field is required`;
}else{
error_month.textContent = ``;
valid = true
}
return valid;
}
MonthInput.addEventListener(`input`, valMonth);
// Validatiob for YearInput
let valYear = () => {
let year = +YearInput.value
valid = false
if(year > 2023){
error_year.textContent = `invalid date`
} else if(year = 0){
error_year.textContent = `pls input a valid no.`
}else if(!YearInput.value){
error_year.textContent = `field is required`;
}else{
error_year.textContent = ``;
valid = true
}
return valid;
}
YearInput.addEventListener(`input`, valYear);
// Age calculation
const calculateAge = () => {
console.log(DayInput.value, MonthInput.value, YearInput.value)
// making sure user input numbers only and not text
let Day = +DayInput.value,
Month = +MonthInput.value,
Year = +YearInput.value;
// getting current date
let CurrentDate = new Date();
//comparing current date withinputted date by user
let diffDay = CurrentDate.getDate() - Day,
diffMonth = CurrentDate.getMonth() - Month + 1,
diffYear = CurrentDate.getFullYear() - Year;
//givine user's age output
AgeOutput.textContent = diffYear;
MonthOutput.textContent = diffMonth;
DayOutput.textContent = diffDay;
}
calc_button.addEventListener(`click`, calculateAge)
// const calculateAge = () => {
// let dateIsValid = validateDay()
// if (!dateIsValid) {
// alert('Date is invalid! Please fix it before continuing.');
// return;
// }
// console.log(DayInput, MonthInput, YearInput);
// let finalDayInput = +DayInput.value;
// let finalMonthInput = +MonthInput.value;
// let finalYearInput = +YearInput.value;
// let todaysDate = new Date()
// let diffYears = todaysDate.getFullYear() - finalYearInput,
// diffMonths = todaysDate.getMonth() - finalMonthInput + 1,
// diffDays = todaysDate.getDate() - finalDayInput;
// DayOutput.textContent = diffDays;
// MonthOutput.textContent = diffMonths;
// AgeOutput.textContent = diffYears;
// console.log(diffYears, diffMonths, diffDays);
// }
// let validateDay = () => {
// let day = +DayInput.value,
// Valid = false;
// if (day > 31 || day <= 0) {
// error_day.textContent = `Put a valid day`;
// } else if (!DayInput.value) {
// error_day.textContent = `This field is required`;
// } else {
// Valid = true;
// error_day.textContent = ``;
// }
// return isValid;
// }
// DayInput.addEventListener(`input`, validateDay)
// calc_button.addEventListener(`click`, calculateAge);