forked from OpenClassrooms-Student-Center/GameOn-website-FR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modal.js
233 lines (202 loc) · 8.79 KB
/
modal.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
223
224
225
226
227
228
229
230
231
232
233
function editNav() {
const x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
// DOM Elements
const modalBg = document.querySelector(".bground");
const modalBtn = document.querySelectorAll(".modal-btn");
const formData = document.querySelectorAll(".formData");
const modalClose = document.querySelector(".close"); // we select the close btn (x)
const modalConfirmation = document.querySelector(".bg-confirm");
const confirmationClose = document.querySelector(".bg-confirm .close"); // we select the close btn (x)
const closeBtn = document.querySelector(".close-confirmation");
// launch modal event
modalBtn.forEach((btn) => btn.addEventListener("click", launchModal));
// launch modal form
function launchModal() {
modalBg.style.display = "block";
}
// launch modal confirmation
function launchModalConfirmation() {
modalConfirmation.style.display = "block";
}
// close modal event
modalClose.addEventListener("click", closeModal); // we listen if the user click on the cross btn, if yes, we close the modal
// close modal function
function closeModal() {
modalBg.style.display = "none"; // hide the modal
}
// close modal Confirmation event
confirmationClose.addEventListener("click", closeModalConfirmation); // we listen if the user click on the cross btn, if yes, we close the confirmation modal
// close modal confirmation function
function closeModalConfirmation() {
modalConfirmation.style.display = "none"; // hide the confirmation modal
}
closeBtn.addEventListener("click", closeModalConfirmation); // we listen if the user click on the close confirmation btn, if yes, we close the confirmation modal
const form = document.querySelector("form");
// we listen the form when user click on submit
form.addEventListener("submit", (event) => {
// we prevent the browser not to execute the default behavior associated with the event (submitting and reloading the page)
event.preventDefault();
if (validate()) {
closeModal();
launchModalConfirmation();
form.reset(); // we reset the values of the form, empty out the fields
resetStyleClasses(); // we remove classes "valid" / "invalid"
}
});
function validate() {
// we get the values of the fields
const firstname = document.getElementById("firstname").value;
const lastname = document.getElementById("lastname").value;
const email = document.getElementById("email").value;
const birthdate = document.getElementById("birthdate").value;
const quantity = document.getElementById("quantity").value;
const location = document.querySelector('input[name="location"]:checked');
const checkbox1 = document.getElementById("checkbox1").checked;
// we select all inputs, in order to show to the user if the field is valid or not
const inputFirstname = document.getElementById("firstname");
const inputLastname = document.getElementById("lastname");
const inputEmail = document.getElementById("email");
const inputBirthdate = document.getElementById("birthdate");
const inputQuantity = document.getElementById("quantity");
const radioBtnIcons = document.querySelectorAll(".location .checkbox-icon");
const inputCheckbox1 = document.querySelector(".cgu .checkbox-icon");
const emailRegExp = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
// ^: Start of the string
// [^\s@]+: One or more characters that are neither spaces nor '@' for the part before the '@'".
// @: the character "@".
// [^\s@]+: One or more characters that are neither spaces nor '@' for the part after the '@'".
// \.: the character "." (dot), that is escaped because it has a special meaning in regex.
// [^\s@]{2,}: indicates that the sequence of characters after the last dot '.' (the domain name extension) must contain at least 2 characters that are neither spaces nor '@' until the end of the string $."
// we select "error zones" to inject texte when we find an error
const errorFirstname = document.getElementById("error-firstname");
const errorLastname = document.getElementById("error-lastname");
const errorEmail = document.getElementById("error-email");
const errorBirthdate = document.getElementById("error-birthdate");
const errorQuantity = document.getElementById("error-quantity");
const errorLocation = document.getElementById("error-location");
const errorCgu = document.getElementById("error-cgu");
let isValid = true;
// Firstname validation
if (firstname.trim() == "") {
errorFirstname.textContent = "Veuillez renseigner votre prénom svp";
inputFirstname.classList.add("invalid");
isValid = false;
} else if (firstname.trim().length < 2) {
errorFirstname.textContent = "Votre prénom est trop court !";
inputFirstname.classList.add("invalid");
inputFirstname.classList.remove("valid");
isValid = false;
} else {
inputFirstname.classList.add("valid");
errorFirstname.textContent = "";
}
// Lastname validation
if (lastname.trim() == "") {
errorLastname.textContent = "Veuillez renseigner votre nom de famille svp";
inputLastname.classList.add("invalid");
isValid = false;
} else if (lastname.trim().length < 2) {
errorLastname.textContent = "Votre nom est trop court !";
inputLastname.classList.add("invalid");
inputLastname.classList.remove("valid");
isValid = false;
} else {
inputLastname.classList.add("valid");
errorLastname.textContent = "";
}
// Email validation
if (!emailRegExp.test(email)) { // = if (emailRegExp.test(email) == false) {
errorEmail.textContent = "Veuillez saisir une adresse e-mail valide svp";
inputEmail.classList.add("invalid");
inputEmail.classList.remove("valid");
isValid = false;
} else {
inputEmail.classList.add("valid");
errorEmail.textContent = "";
}
// Quantity validation
if (quantity.trim() == "" || quantity.trim() > 99) {
errorQuantity.textContent =
"Veuillez saisir un nombre valide de tournois svp";
inputQuantity.classList.add("invalid");
inputQuantity.classList.remove("valid");
isValid = false;
} else {
inputQuantity.classList.add("valid");
inputQuantity.classList.remove("invalid");
errorQuantity.textContent = "";
}
// Location validation
if (location == null) {
errorLocation.textContent = "Veuillez choisir une ville svp";
for (let i = 0; i < radioBtnIcons.length; i++) {
radioBtnIcons[i].style.border = "2px solid red";
}
} else {
errorLocation.textContent = "";
for (let i = 0; i < radioBtnIcons.length; i++) {
radioBtnIcons[i].style.border = "";
}
}
// Terms and Conditions validation
if (!checkbox1) {
errorCgu.textContent = "Veuillez accepter les conditions d'utilisation svp";
inputCheckbox1.style.border = "2px solid red";
isValid = false;
} else {
errorCgu.textContent = "";
inputCheckbox1.style.border = "2px solid rgb(0, 255, 21)";
}
//////////////////////////////////////////
// Birthdate Validation //
//////////////////////////////////////////
// creating Date objects
const birthdateUser = new Date(birthdate); // birthdate of the user
const today = new Date(); // the current date and time
const thirteenYearsAgo = new Date();
thirteenYearsAgo.setFullYear(today.getFullYear() - 13); // set the value of the object 13 years before the current date
const _123YearsAgo = new Date();
_123YearsAgo.setFullYear(today.getFullYear() - 123); // set the value of the object 123 years before the current date
if (birthdate == "" || birthdate == null) {
errorBirthdate.textContent = "Veuillez entrer votre date de naissance svp";
inputBirthdate.classList.add("invalid");
inputBirthdate.classList.remove("valid");
isValid = false;
} else if (birthdateUser < _123YearsAgo) {
errorBirthdate.textContent =
"Veuillez entrer une date de naissance valide svp";
inputBirthdate.classList.add("invalid");
inputBirthdate.classList.remove("valid");
isValid = false;
} else if (birthdateUser > thirteenYearsAgo) {
errorBirthdate.textContent =
"Désolé, vous n'avez pas l'âge requis pour participer !";
inputBirthdate.classList.add("invalid");
inputBirthdate.classList.remove("valid");
isValid = false;
} else if (
birthdateUser >= _123YearsAgo &&
birthdateUser <= thirteenYearsAgo
) {
errorBirthdate.textContent = "";
inputBirthdate.classList.add("valid");
inputBirthdate.classList.remove("invalid");
}
// If all validations are OK, the form is valid
return isValid;
}
// we remove classes "valid" / "invalid" when form is valid
function resetStyleClasses() {
const fields = document.querySelectorAll('.text-control');
const inputCheckbox1 = document.querySelector(".cgu .checkbox-icon");
fields.forEach(function(field) {
field.classList.remove('valid', 'invalid');
});
inputCheckbox1.style.border = "";
}