From 274e5544a6247726a0235e5fd7b011184c774acc Mon Sep 17 00:00:00 2001 From: kumud-05 Date: Sun, 5 Jul 2026 17:22:51 +0530 Subject: [PATCH] fix: add email validation during account registration --- backend/controllers/authController.js | 9 +++++++++ frontend/src/pages/Auth/SignUp.jsx | 20 +++++++++++++++----- frontend/src/utils/helper.js | 4 ++-- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/backend/controllers/authController.js b/backend/controllers/authController.js index 94188c2..75ef5a3 100644 --- a/backend/controllers/authController.js +++ b/backend/controllers/authController.js @@ -36,6 +36,15 @@ const registerUser = async (req, res) => { try { const { name, email, password, profileImageUrl } = req.body; + const emailRegex = /^[^\s@]+@[^\s@]+\.[A-Za-z]{2,}$/; + + if (!emailRegex.test(email)) { + return res.status(400).json({ + success: false, + message: "Please enter a valid email address.", + }); + } + const { valid, errors } = validatePassword(password); if (!valid) { return res.status(400).json({ success: false, message: errors[0] }); diff --git a/frontend/src/pages/Auth/SignUp.jsx b/frontend/src/pages/Auth/SignUp.jsx index 38faf56..411b98c 100644 --- a/frontend/src/pages/Auth/SignUp.jsx +++ b/frontend/src/pages/Auth/SignUp.jsx @@ -171,11 +171,21 @@ const SignUp = ({ setCurrentPage }) => { /> setEmail(target.value)} - label="Email Address" - placeholder="your@email.com" - type="text" + value={email} + onChange={({ target }) => { + const value = target.value; + setEmail(value); + + if ( + error === "Please enter a valid email address" && + validateEmail(value) + ) { + setError(""); + } + }} + label="Email Address" + placeholder="your@email.com" + type="email" /> { - const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; +export const validateEmail = (email) => { + const regex = /^[^\s@]+@[^\s@]+\.[A-Za-z]{2,}$/; return regex.test(email); };