-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
106 lines (94 loc) · 3.39 KB
/
main.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 now = new Date();
let birthDay= new Date();
let myButton = document.querySelector("button");
let day = document.getElementById("inDay");
let month = document.getElementById("inMonth");
let year = document.getElementById("inYear");
let daysOfMonths = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
/* better way for filed
year.setAttribute("min", 1);
year.setAttribute("max", now.getFullYear());
month.setAttribute("min", "1");
month.setAttribute("max", "12");
day.setAttribute("min", "1");
day.setAttribute("max", "31");
// */
myButton.onclick = ()=>{
validField(year);
validField(month);
validField(day);
let isYearValid = validation(year, now.getFullYear());
let isMonthValid = validation(month, 12);
let isDayValid = validation(day, daysOfMonths[month.value-1] || 31);
if ( isYearValid && isMonthValid && isDayValid){
birthDay = new Date(year.value, month.value - 1, day.value);
let age = ageCalc(now, birthDay);
counter(document.getElementById("yearsNo"), age.ageInYears);
counter(document.getElementById("monthsNo"), age.ageInMonths);
counter(document.getElementById("daysNo"), age.ageInDays);
}
};
// validation
// i use this long way to check while the user input and when he Click
function validation(field, biggerThan, smallerThan = 1, wrongEnterMsg = "Please Enter Valid Value", emptyMsg = "Filed is Required"){
let valid = false;
valid = validationMethods(field, biggerThan, smallerThan, wrongEnterMsg, emptyMsg);
field.onblur = ()=>validationMethods(field, biggerThan, smallerThan, wrongEnterMsg, emptyMsg);
return valid;
}
function validationMethods(field, biggerThan, smallerThan, wrongEnterMsg, emptyMsg){
if(field.value =="")
notValidField(field, emptyMsg);
else if(field.value>biggerThan || field.value<smallerThan)
notValidField(field, wrongEnterMsg);
else {
validField(field);
return true;
}
return false;
}
function notValidField(field, msg){
let parents = document.querySelector(`.${field.getAttribute("id")} p`);
// color the filed
parents.classList.add("not-valid-color");
field.classList.add("not-valid-boarder");
document.querySelector(`.${field.getAttribute("id")} label`).classList.add("not-valid-color");
// create the element and appended
let mySpan = document.createElement("span");
mySpan.appendChild(document.createTextNode(msg));
parents.appendChild(mySpan);
field.onfocus = ()=>{
validField(field);
}
}
function validField(field){
let parents = document.querySelector(`.${field.getAttribute("id")} p`);
parents.classList.remove("not-valid-color");
field.classList.remove("not-valid-boarder");
document.querySelector(`.${field.getAttribute("id")} label`).classList.remove("not-valid-color");
parents.innerHTML = "";
}
// counter
function counter(element, finish){
let counter=0;
element.innerText = counter;
let timer = setInterval(()=>{
if(element.innerText==finish) return clearInterval(timer);
element.innerText++;
}, 60)
}
function ageCalc(now, birthDay){
let ageInYears = now.getFullYear() - birthDay.getFullYear();
let ageInMonths = now.getMonth() - birthDay.getMonth();
let ageInDays = now.getDate() - birthDay.getDate();
if(ageInDays<0) {
ageInMonths--;
ageInDays += new Date(now.getFullYear(), now.getMonth(), 0).getDate();
// ageInDays /=0;
}
if(ageInMonths<0){
ageInYears--;
ageInMonths+=12;
}
return {ageInYears, ageInMonths, ageInDays};
}