From 40ab327f7679a25e77e6d7233d1c43f5fd3e79f7 Mon Sep 17 00:00:00 2001 From: Mugunth <162331271+Mugunth140@users.noreply.github.com> Date: Sat, 19 Oct 2024 09:48:01 +0530 Subject: [PATCH] Created emailValidator.js --- emailValidator.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 emailValidator.js diff --git a/emailValidator.js b/emailValidator.js new file mode 100644 index 00000000..002636ec --- /dev/null +++ b/emailValidator.js @@ -0,0 +1,14 @@ +function validateEmail(email) { + const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; + return re.test(String(email).toLowerCase()); +} +document.addEventListener("DOMContentLoaded", () => { + const form = document.querySelector("form"); + form.addEventListener("submit", function(event) { + const emailInput = document.querySelector("input[name='username']"); + if (!validateEmail(emailInput.value)) { + alert("Please enter a valid email address."); + event.preventDefault(); + } + }); +});