-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
118 lines (57 loc) · 2.18 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
let calculatorForm = document.forms.calculatorform
let userInput = document.querySelectorAll("input");
let outputMyAge = document.querySelectorAll(".result_container");
let userYear = document.getElementById("useryear");
let userMonth= document.getElementById("usermonth");
let userDay = document.getElementById("userday");
let mainHead= document.getElementById("main_heading");
mainHead.style.color = "blue";
const date= new Date()
const currentYear = date.getFullYear();
const currentMonth = date.getMonth() +1;
const currentDay = date.getDay();
const together = [currentYear,currentMonth,currentDay];
//console.log(together);
calculatorForm.onsubmit= (e)=>{
e.preventDefault();
console.log(e.target);
let formErr = {
dayErr: "",
monthErr: "",
yearErr: "",
}
let day = calculatorForm.day.value.trim();
let month = calculatorForm.month.value.trim();
let year = calculatorForm.year.value.trim();
if(day == "" || day > 31 ){
formErr.dayErr = "must be a valid day";
}else if(month.value == ""){
formErr.monthErr= "must be a valid month";
}else if(month < 1 || month > 12){
formErr.monthErr = "must be a valid month";
}else if(year.value == ""){
formErr.yearErr = "year is required";
}else if(year < 1 || year > 2023){
formErr.yearErr = "must be a valid year";
}else{
console.log({day,month,year});
calculateUserAge();
}
let errElement = document.querySelectorAll(".err");
for(let i=0; i < errElement.length; i++){
let errElements = errElement[i]
console.log(formErr)
errElements.innerHTML = formErr[""+errElements.dataset.err+""]
}
function calculateUserAge() {
let userAge = currentYear - year
userYear.style.color="blue";
userYear.innerHTML= userAge
let userAgeMonth =currentMonth - month + 1
userMonth.style.color ="blue";
userMonth.innerHTML= userAgeMonth
let userBirthDay= currentDay - day + 31
userDay.style.color="blue";
userDay.innerHTML= userBirthDay
}
}