From a9483c7f2296739980cfa5879633bf095a3f2bbd Mon Sep 17 00:00:00 2001 From: midblep Date: Fri, 4 Feb 2022 20:13:10 +0100 Subject: [PATCH] form fix! --- src/lib/elements/Form.svelte | 45 +++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/src/lib/elements/Form.svelte b/src/lib/elements/Form.svelte index cd3a9ac..f51b27d 100644 --- a/src/lib/elements/Form.svelte +++ b/src/lib/elements/Form.svelte @@ -4,15 +4,47 @@ let email = ""; let message = ""; let notify; + let error; + + const validateEmail = (email) => { + return String(email) + .toLowerCase() + .match( + /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ + ); + }; + + $: allowSend = validate(email, message); + + function validate(email, message) { + if(email == "" || message == "") { + error = null; + return false; + } + + if(!validateEmail(email)) { + error = "Invalid email format!"; + return false; + } + + if(message.length < 10) { + error = "Message body too short!"; + return false; + } + + error = null; + return true; + } function send() { - email = ""; + email = ""; message = ""; notify = "Email sent!"; + return; } -
+
- +