Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added email validation #122

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/components/contact/Hello.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ import Heading from "@theme/Heading"
import toast, {Toaster} from "react-hot-toast"
import Grid from "@site/static/images/about/grid-large.svg"
import LinkButton from "../shared/LinkButton"
import {analyticsHandler} from "@site/src/utils"
import {analyticsHandler, validateEmail} from "@site/src/utils"
import {Theme, radioOptions, zapierLink} from "@site/src/constants"

const Hello = (): JSX.Element => {
const [email, setEmail] = useState<string>("")
const [message, setMessage] = useState<string>("")
const [stage, setStage] = useState<string>("")
const [isValid, setIsValid] = useState<boolean>(true)

const sendData = useCallback(async () => {
if (!validateEmail(email)) {
setIsValid(false)
return
}
const response = await fetch(zapierLink, {
method: "POST",
body: JSON.stringify({
Expand All @@ -31,6 +36,7 @@ const Hello = (): JSX.Element => {
setEmail("")
setMessage("")
setStage("")
setIsValid(true)
}
}, [email, message, stage])

Expand All @@ -56,10 +62,15 @@ const Hello = (): JSX.Element => {
name="email"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
className="border border-solid border-tailCall-border-light-500 rounded-lg font-space-grotesk h-11 w-[95%] sm:w-[480px] p-SPACE_03 text-content-small outline-none focus:border-x-tailCall-light-700"
onChange={(e) => {
setEmail(e.target.value)
if (!isValid) setIsValid(true)
}}
className={`border border-solid border-tailCall-border-light-500 rounded-lg font-space-grotesk h-11 w-[95%] sm:w-[480px]
p-SPACE_03 text-content-small outline-none focus:border-x-tailCall-light-700 ${isValid ? "is-valid" : "is-invalid"}`}
placeholder="[email protected]"
/>
{!isValid && <div className="text-red-400">Please enter a valid email.</div>}
</div>

<div className="flex flex-col space-y-SPACE_02">
Expand Down
5 changes: 5 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ export const setBodyOverflow = (value: "initial" | "hidden") => {
export const getSearchInputRef = () => {
return document.getElementById("search_input_react")
}

export const validateEmail = (email: string) => {
const regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
return regex.test(email)
}
Loading