-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
222 lines (176 loc) · 5.48 KB
/
index.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// MANAGING THE SLIDER SECTION
const firstTab = $(".slider__tab:first-child");
var tabID = firstTab.attr("id");
var imgID = tabID + "__img";
var contentID = tabID + "__content";
$("#" + tabID).addClass("active");
$("#" + imgID).addClass("active");
$("#" + contentID).addClass("active");
$(".slider__tab").click(function () {
$(".slider__tab, .slider__img, .slider__content").removeClass("active");
tabID = $(this).attr("id");
imgID = tabID + "__img";
contentID = tabID + "__content";
$(this).addClass("active");
$("#" + imgID).addClass("active");
$("#" + contentID).addClass("active");
});
// MANAGING THE DROPDOWN SELECTOR
$(".form__select").on("click", function () {
$(this).toggleClass("open");
})
$(".form__option").on("click", function () {
$(".form__option").removeClass("selected");
$(this).addClass("selected");
const selected = $(this).text();
$(this).parent().siblings(".form__select-trigger").children("span").text(selected);
$(this).parent().siblings(".form__select-input").val(selected);
})
// MANAGING THE PEOPLE COUNT SELECTOR
var peopleCount = 4;
$("#people").val(peopleCount);
$("#people-count").text(peopleCount + " people");
$("#decrement").click(function () {
if (peopleCount > 1) {
peopleCount--;
$("#people").val(peopleCount);
}
if (peopleCount > 1) {
$("#people-count").text(peopleCount + " people");
}
else {
$("#people-count").text(peopleCount + " person");
}
})
$("#increment").click(function () {
peopleCount++;
$("#people").val(peopleCount);
$("#people-count").text(peopleCount + " people");
})
// SUBMITTING THE FORM
$(".form").on("submit", function (event) {
event.preventDefault();
const nameValid = validateName($("#name").val());
const emailValid = validateEmail($("#email").val());
const dateValid = validateDate($("#day").val(), $("#month").val(), $("#year").val());
const timeValid = validateTime($("#hour").val(), $("#minute").val(), $("#day").val(), $("#month").val(), $("#year").val());
const formValid = nameValid && emailValid && dateValid && timeValid;
if (formValid) {
event.target.reset();
$(".success").addClass("active");
}
})
// CLOSING THE SUCCESS MESSAGE
$(".success__close").on("click", function () {
$(this).parent().parent().removeClass("active");
})
// CHECKING IF AN INPUT IS NUMERIC
function isNumeric(input) {
return (input - 0) == input && input.length > 0;
}
// VALIDATING NAME
function validateName(name) {
const nameField = $("#name-container, #name");
if (name === "") {
nameField.addClass("error");
return false;
}
else {
nameField.removeClass("error");
return true;
}
}
// VALIDATING EMAIL
function isEmail(email) {
const re = /\S+@\S+\.\S+/;
return re.test(email);
}
function validateEmail(email) {
const emailField = $("#email-container, #email");
if (email === "") {
emailField.addClass("error").removeClass("invalid");
return false;
}
else {
if (isEmail(email)) {
emailField.removeClass("error").removeClass("invalid");
return true;
}
else {
emailField.addClass("error").addClass("invalid");
return false;
}
}
}
// VALIDATING DATE
function daysInMonth(month, year) {
switch (month) {
case 1:
return (year % 4 === 0 && year % 100) || year % 400 === 0 ? 29 : 28;
case 8: case 3: case 5: case 10:
return 30;
default:
return 31
}
}
function isValidDate(day, month, year) {
month = parseInt(month, 10) - 1;
return month >= 0 && month < 12 && day > 0 && day <= daysInMonth(month, year);
}
function validateDate(day, month, year) {
const dateField = $("#date-label, #day, #month, #year");
const dateValid = isValidDate(day, month, year);
const inputDate = new Date(moment(new Date(year, month - 1, day)).format("LL"));
const currentDate = new Date(moment(new Date()).format("LL"));
const datePassed = inputDate.getTime() < currentDate.getTime();
if (day === "" || month === "" || year === "") {
dateField.addClass("error").removeClass("invalid");
return false;
}
else {
if (!dateValid || datePassed) {
dateField.addClass("error").addClass("invalid");
return false;
}
else {
dateField.removeClass("error").removeClass("invalid");
return true;
}
}
}
// VALIDATING TIME
function validateTime(hour, minute, day, month, year) {
const timeField = $("#time-label, #hour, #minute");
const timeValid = validateHour(hour) && validateMinute(minute);
const timePassed = new Date(year, month - 1, day, hour, minute).getTime() <= Date.now();
if (hour === "" || minute === "") {
timeField.addClass("error").removeClass("invalid");
return false;
}
else {
if (!timeValid || timePassed) {
timeField.addClass("error").addClass("invalid");
return false;
}
else {
timeField.removeClass("error").removeClass("invalid");
return true;
}
}
}
function validateHour(hour) {
if (hour != "" && (hour < 1 || hour > 12)) {
return false;
}
else {
return true;
}
}
function validateMinute(minute) {
if (minute != "" && (minute < 0 || minute > 59)) {
return false;
}
else {
return true;
}
}