-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
93 lines (75 loc) · 2.91 KB
/
index.js
File metadata and controls
93 lines (75 loc) · 2.91 KB
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
const inputs = document.querySelectorAll('input');
const dayInp = document.querySelector('#day');
const monInp = document.querySelector('#month');
const yearInp = document.querySelector('#year');
const dayOut = document.querySelector('#D');
const monOut = document.querySelector('#M');
const yearOut = document.querySelector('#Y');
const form = document.querySelector('form');
const btn = document.querySelector('button');
let date = new Date();
let day = date.getDate();
let month = 1+date.getMonth();
let year = date.getFullYear();
const months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,31];
function validate(){
let validator = true;
inputs.forEach((i) =>{
const parent = i.parentElement;
// alert('Hey');
if(!i.value){
i.style.borderColor = 'red';
parent.querySelector('p').innerHTML = 'This field is required!!';
parent.querySelector('label').style.color = 'red';
// dayInp.parentElement.querySelector('button').top = '45%'
validator = false;
}
else if(dayInp.value > 31 || dayInp.value < 1){
dayInp.style.borderColor = 'red';
dayInp.parentElement.querySelector('p').innerHTML = 'Must be a valid day!!';
dayInp.parentElement.querySelector('label').style.color = 'red';
validator = false;
} else if(monInp.value > 12 || monInp.value < 1){
monInp.style.borderColor = 'red';
monInp.parentElement.querySelector('p').innerHTML = 'Must be a valid month!!';
monInp.parentElement.querySelector('label').style.color = 'red';
validator = false;
} else if(yearInp.value > year){
yearInp.style.borderColor = 'red';
yearInp.parentElement.querySelector('p').innerHTML = 'Must be a in past!!';
yearInp.parentElement.querySelector('label').style.color = 'red';
validator = false;
} else if(yearInp.value < 0){
yearInp.style.borderColor = 'red';
yearInp.parentElement.querySelector('p').innerHTML = 'Must be a valid year!!';
yearInp.parentElement.querySelector('label').style.color = 'red';
validator = false;
}
else {
i.style.borderColor = 'blue';
parent.querySelector('p').innerHTML = '';
validator = true;
}
});
return validator;
}
function handler(e){
e.preventDefault();
if(validate()){
if(dayInp.value > day){
day += months[month-1];
month = month - 1;
}
if(monInp.value > month){
month = month + 12;
year = year - 1;
}
const d = day - dayInp.value;
const m = month - monInp.value;
const y = year - yearInp.value;
dayOut.innerHTML = d;
monOut.innerHTML = m;
yearOut.innerHTML = y;
}
}
form.addEventListener("submit", handler);