From c6f0d851c2da49667224e0c86176d56cb1c07778 Mon Sep 17 00:00:00 2001 From: log101 Date: Thu, 18 Jul 2024 20:00:48 +0300 Subject: [PATCH] feat: add client side validation for file input --- src/pages/index.astro | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/pages/index.astro b/src/pages/index.astro index 534b56b..0edce8b 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -170,6 +170,24 @@ import Layout from "../layouts/Layout.astro"; return; } + const inputEl = document.getElementById( + "photo-selector" + ) as HTMLInputElement | null; + + if (!inputEl) return; + + const files = inputEl.files; + + if (!files) return; + + if (files.length > 0) { + if (files[0].size > 4 * 1024 * 1024) { + inputEl.setCustomValidity("Dosya boyutu 4 MB'den küçük olmalıdır."); + inputEl.reportValidity(); + return; + } + } + const submitButton = document.getElementById( "submit-button" ) as HTMLButtonElement;