From 4fd38af857299b94fdebd39f3360b2c4001fbdd2 Mon Sep 17 00:00:00 2001 From: TobiasPartzsch Date: Sun, 13 Jul 2025 10:30:07 +0200 Subject: [PATCH] feat: Add client-side validation to login and signup forms --- app/app.js | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/app/app.js b/app/app.js index f688af96a..d083510bc 100644 --- a/app/app.js +++ b/app/app.js @@ -50,8 +50,9 @@ async function createVideoDraft() { } async function login() { - const email = document.getElementById('email').value; - const password = document.getElementById('password').value; + const authData = validateAuthForm(); + if (!authData) return; + const { email, password } = authData; try { const res = await fetch('/api/login', { @@ -80,8 +81,9 @@ async function login() { } async function signup() { - const email = document.getElementById('email').value; - const password = document.getElementById('password').value; + const authData = validateAuthForm(); + if (!authData) return; + const { email, password } = authData; try { const res = await fetch('/api/users', { @@ -299,3 +301,30 @@ async function deleteVideo() { alert(`Error: ${error.message}`); } } + +function validateAuthForm() { + const email = document.getElementById('email').value.trim(); + const password = document.getElementById('password').value.trim(); + + if (!email) { + alert('Email is required.'); + return null; + } + + if (!email.includes('@') || !email.includes('.')) { + alert('Please enter a valid email address.'); + return null; + } + + if (!password) { + alert('Password is required.'); + return null; + } + + if (password.length < 6) { + alert('Password must be at least 6 characters long.'); + return null; + } + + return { email, password }; +} \ No newline at end of file