From fbbebe76955f6aab3a555885d0060527e5fcea20 Mon Sep 17 00:00:00 2001 From: Vivek Vishal Date: Sun, 1 Sep 2024 18:09:49 +0530 Subject: [PATCH 1/6] chore: add client-side validation for email and pitcture url Signed-off-by: Vivek Vishal --- .../Community/Web-based-from/index.js | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/sections/Community/Web-based-from/index.js b/src/sections/Community/Web-based-from/index.js index dc186a7aa3f1..b881f3c04fae 100644 --- a/src/sections/Community/Web-based-from/index.js +++ b/src/sections/Community/Web-based-from/index.js @@ -7,6 +7,38 @@ import { Field, Formik, Form } from "formik"; import axios from "axios"; import { Link } from "gatsby"; +const validateEmail = (value) => { + let error; + + if (!value) { + error = "Required"; + } else if (!/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/.test(value)) { + error = "Please enter a valid email address"; + } + return error; +}; + +const validatePictureUrl = (value) => { + let error; + if (value) { + + if (value.startsWith("data:")) { + error = "Data URIs are not allowed. Please provide a URL to an image file."; + } else { + try { + new URL(value); + const allowedImageExtensions = ["jpg", "jpeg", "png", "webp", "svg"]; + const extension = value.split(".").pop().toLowerCase(); + if (!allowedImageExtensions.includes(extension)) { + error = "URL must point to an image file (jpg, jpeg, png, svg or webp)."; + } + } catch (_) { + error = "Please enter a valid URL."; + } + } + } + return error; +}; const WebBasedForm = () => { @@ -99,13 +131,14 @@ const WebBasedForm = () => { nextStep(); }} > + {({ errors, touched }) => (
e.target.setCustomValidity("Please fill-in this field")} onInput={e => e.target.setCustomValidity("")} /> e.target.setCustomValidity("Please fill-in this field")} onInput={e => e.target.setCustomValidity("")} /> - e.target.setCustomValidity("Please fill-in this field")} onInput={e => e.target.setCustomValidity("")} /> + {errors.email && touched.email &&

{errors.email}

} @@ -149,10 +182,12 @@ const WebBasedForm = () => { - + + {errors.picture && touched.picture &&
{errors.picture}
}

Please provide a link to your profile photo. Profile photos are used for community member profiles of longstanding community members.