Skip to content

Commit

Permalink
issue OpenClassrooms-Student-Center#2: finalisation de la validation …
Browse files Browse the repository at this point in the history
…du formulaire
  • Loading branch information
manelgasmi committed Jul 31, 2024
1 parent cc1a2ea commit 476e8cb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
7 changes: 4 additions & 3 deletions starterOnly/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ <h1 class="hero-headline">
<div class="content">
<button class="close"></button>
<div class="modal-body">
<form
<form novalidate
name="reserve"
action="index.html"
method="get"
onsubmit="return validate();"
onsubmit="return validate(event);"
>
<div class="formData">
<label for="first">Prénom</label><br />
Expand All @@ -77,6 +77,7 @@ <h1 class="hero-headline">
type="text"
id="last"
name="last"
minlength="2"
/><br />
</div>
<div class="formData">
Expand Down Expand Up @@ -189,7 +190,7 @@ <h1 class="hero-headline">
type="checkbox"
id="checkbox1"
checked

required
/>
<label class="checkbox2-label" for="checkbox1">
<span class="checkbox-icon"></span>
Expand Down
54 changes: 54 additions & 0 deletions starterOnly/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,59 @@ btnClose.addEventListener("click", closeModal);
// Close modal form
function closeModal() {
modalbg.style.display = "none";
}

// form validation
function validate(event) {
event.preventDefault();

// First name validation
const firstNameInput = document.getElementById("first");
const firstName = firstNameInput.value;
if (firstName.length < 2) {
console.log("Erreur prénom doit être minimum 2 lettres");
}

// Last name validation
const lastNameInput = document.getElementById("last");
const lastName = lastNameInput.value;
if (lastName.length < 2) {
console.log("Erreur nom doit être minimum 2 lettres");
}

// email validation
const emailInput = document.getElementById("email");
let email = emailInput.value;
let emailRegExp = new RegExp("[a-z0-9._-]+@[a-z0-9._-]+.[a-z0-9._-]+");
if (emailRegExp.test(email)) {
console.log("Email valide");
} else {
console.log("Erreur email non valide");
}

// number of Competition validation
const nbrCompetitions = document.getElementById("quantity");
const nbr = nbrCompetitions.value;
if (nbr < 0 || nbr > 99 || !nbr) {
console.log("Erreur nombre de compétition doit être compris entre 0 et 99");
}

// input radio validation
let radioInputs = document.querySelectorAll('input[name = "location"]');
let locationChecked = false;
for (let i = 0; i < radioInputs.length; i++) {
if (radioInputs[i].checked) {
locationChecked = true;
break;
}
}
if(locationChecked === false) {
console.log("Erreur vous devez choisir une localisation");
}

// general conditions box validation
const generalConditions = document.getElementById("checkbox1");
if (generalConditions.checked === false) {
console.log("Erreur vous devez accepter les conditions générales");
}
}

0 comments on commit 476e8cb

Please sign in to comment.