From 2de798f9cae0695fd6bad6a01f0e8f52fb4aa209 Mon Sep 17 00:00:00 2001 From: Kevin Mazats Date: Wed, 3 Feb 2021 18:11:39 +0100 Subject: [PATCH] isue #2 global form validation --- starterOnly/form.js | 50 ++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/starterOnly/form.js b/starterOnly/form.js index 5bdcf58c9b..ce95806773 100644 --- a/starterOnly/form.js +++ b/starterOnly/form.js @@ -1,3 +1,9 @@ +// DOM elements + +const form = document.querySelector("form"); + +//inputs validations + function firstValidation() { let inputValue = document.getElementById("first").value; if (inputValue !== null && inputValue.length > 2) return true; @@ -11,33 +17,49 @@ function lastValidation() { } function emailValidation() { - let regex = /^([a-z0-9_\.-]+\@[\da-z\.-]+\.[a-z\.]{2,6})$/; - let inputValue = document.getElementById("email").value; - return regex.test(inputValue); + let regex = /^([a-z0-9_\.-]+\@[\da-z\.-]+\.[a-z\.]{2,6})$/; + let inputValue = document.getElementById("email").value; + return regex.test(inputValue); } function quantityValidation() { - let regex = /^[0-9]+$/; - let inputValue = document.getElementById("quantity").value; - return regex.test(inputValue); + let regex = /^[0-9]+$/; + let inputValue = document.getElementById("quantity").value; + return regex.test(inputValue); } function locationValidation() { - let radioButtons = document.querySelectorAll(".checkbox-input[type=radio]"); - for(let radio of radioButtons){ - if(radio.checked === true) return true; - } - return false; + let radioButtons = document.querySelectorAll(".checkbox-input[type=radio]"); + for (let radio of radioButtons) { + if (radio.checked === true) return true; + } + return false; } function checkboxValidation() { - let inputValue = document.getElementById("checkbox1").checked; - return inputValue; + let inputValue = document.getElementById("checkbox1").checked; + return inputValue; } document .getElementById("button") .addEventListener("click", function formValidation(event) { event.preventDefault(); - console.log(checkboxValidation()); + let isValid = true; + if (!firstValidation()) { + isValid = false; + } else if (!lastValidation()) { + isValid = false; + } else if (!emailValidation()) { + isValid = false; + } else if (!quantityValidation()) { + isValid = false; + } else if (!locationValidation()) { + isValid = false; + } else if (!checkboxValidation()) { + isValid = false; + } + if (isValid) { + form.submit(); + } });