From 115b95675fbdbc5b27247f01b44d254b52edf644 Mon Sep 17 00:00:00 2001 From: tighei Date: Fri, 1 Mar 2024 19:25:24 +0100 Subject: [PATCH] Refactor form validation and add email validation --- .vscode/launch.json | 11 ----------- MAIN.js | 24 ++++++++++++++++++++++-- index.html | 8 ++++++-- style.css | 18 ++++++++++++------ 4 files changed, 40 insertions(+), 21 deletions(-) delete mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index bed780b..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - - - - ] -} \ No newline at end of file diff --git a/MAIN.js b/MAIN.js index 2d90c66..c45c3e4 100644 --- a/MAIN.js +++ b/MAIN.js @@ -42,11 +42,16 @@ const submitBtn = document.getElementById("submit-btn"); const passwordInput = document.querySelector("#password"); const retypePasswordInput = document.querySelector("#password-con"); const messageElement = document.getElementById("Message"); +const email = document.querySelector("#email"); async function isValidPassword(password) { const reg = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}$/; return reg.test(password); } +function isValidEmail(email) { + const reg = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; + return reg.test(email); +} function confirmPasswordMatch() { const password = passwordInput.value; @@ -73,12 +78,27 @@ function checkPasswordMatch() { passwordInput.addEventListener("input", confirmPasswordMatch); retypePasswordInput.addEventListener("input", confirmPasswordMatch); } -checkPasswordMatch(); submitBtn.addEventListener("click", async (e) => { const isPasswordValid = await isValidPassword(passwordInput.value); const doPasswordsMatch = confirmPasswordMatch(); - if (!isPasswordValid || !doPasswordsMatch) { + if ( + !isPasswordValid || + !doPasswordsMatch || + !isValidEmail(emailInput.value) + ) { e.preventDefault(); } }); +function checkEmailValidity() { + email.addEventListener("input", (event) => { + if (!isValidEmail(email.value)) { + email.setCustomValidity("Invalid email address!"); + } else { + email.setCustomValidity(""); + } + }); +} + +checkEmailValidity(); +checkPasswordMatch(); diff --git a/index.html b/index.html index c6607dd..ff71467 100644 --- a/index.html +++ b/index.html @@ -25,7 +25,7 @@

Tigheni

Join Us!

-
+

Let's do this!

@@ -34,6 +34,7 @@

Let's do this!

type="text" id="first-name" name="f-name" + minlength="3" placeholder="YOUR FIRST NAME" />
@@ -44,6 +45,7 @@

Let's do this!

type="text" id="last-name" name="l-name" + minlength="3" placeholder="YOUR LAST NAME" />
@@ -59,7 +61,7 @@

Let's do this!

Let's do this! Let's do this!