Skip to content

Commit

Permalink
feat OpenClassrooms-Student-Center#4 : Ajout d'une modal affichant un…
Browse files Browse the repository at this point in the history
… message de confirmation a la validation du formulaire
  • Loading branch information
HammerPanzer92 committed Dec 21, 2023
1 parent a1f3449 commit cd722bd
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 228 deletions.
8 changes: 8 additions & 0 deletions CSS/modal.css
Original file line number Diff line number Diff line change
Expand Up @@ -541,3 +541,11 @@ footer {
}
}

/*Valid modal*/
.validModal_content{
min-height: 737px;
}

.validModal p{
text-align: center;
}
15 changes: 15 additions & 0 deletions JS/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ dateInput.max = dateJour.getFullYear() + "-" + (dateJour.getMonth() + 1) + "-" +
const modalbg = document.querySelector(".bground");
const modalBtn = document.querySelectorAll(".modal-btn");
const formData = document.querySelectorAll(".formData");
const validModalBg = document.querySelector(".validModal");
const validModal_btn = document.getElementById("validModal_btn");

// launch modal event
modalBtn.forEach((btn) => btn.addEventListener("click", launchModal));
Expand All @@ -32,3 +34,16 @@ function closeModal() {
modalbg.style.display = "none";
}

/**
* Launch the modal for the valid message
*/
function launchValidModal() {
validModalBg.style.display = "block";
}

/**
* Close the modal for the valid message
*/
function closeValidModal() {
validModalBg.style.display = "none";
}
38 changes: 20 additions & 18 deletions JS/validationForm.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const form = document.querySelector("form");

/**
* Validate the form data
* @returns {boolean} true if the form is valid, false otherwise
Expand Down Expand Up @@ -26,45 +28,33 @@ function validate() {
switch (input.type) {
//For input of type text (corresponding to first and last name field)
case "text":
if(!checkString(input)){
check = false;
}
check = checkString(input);
break;

//For input of type mail (corresponding to the E-mail field)
case "email":
if(!checkMail(input)){
check = false;
}
check = checkMail(input);
break;

//For input of type date (for the birthdate field)
case "date":
if(!checkDate(input)){
check = false;
}
check = checkDate(input);
break;

//For input of type number (for the quantity field)
case "number":
if(isNaN(input.value)){
check = false;
}
check = !isNaN(input.value);
break;

//For input of type radios
case "radio":
const listRadios = formData[i].querySelectorAll("input");
if(!checkRadios(listRadios)){
check = false;
}
check = checkRadios(listRadios)
break;

//For input of type checkbox (only one necessary)
case "checkbox":
if(!input.checked){
check = false;
}
check = input.checked;
break;
default:
break;
Expand All @@ -80,3 +70,15 @@ function validate() {
//If everything has been checked, valid will be true, else it will be false
return valid;
}

//Handle the submit event of the form
form.addEventListener("submit",function (e) {
//preventDefault to avoid default behavior (sending the request)
e.preventDefault();
if(validate()){
//We close the form modal to replace it with the confirmation one
closeModal();
launchValidModal();
form.reset();
}
});
Loading

0 comments on commit cd722bd

Please sign in to comment.