Skip to content

Commit 6582ff5

Browse files
committed
feat: validate form fields on blur
1 parent 698f0a0 commit 6582ff5

File tree

1 file changed

+22
-0
lines changed
  • app/[locale]/enterprise/_components/ContactForm

1 file changed

+22
-0
lines changed

app/[locale]/enterprise/_components/ContactForm/index.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,24 @@ const EnterpriseContactForm = ({ strings }: EnterpriseContactFormProps) => {
103103
}
104104
}
105105

106+
const handleBlur =
107+
(field: keyof FormState) =>
108+
(e: React.FocusEvent<HTMLInputElement | HTMLTextAreaElement>) => {
109+
const value = e.target.value
110+
111+
if (field === "email") {
112+
const emailError = validateEmail(value)
113+
if (emailError) setErrors((prev) => ({ ...prev, email: emailError }))
114+
return
115+
}
116+
if (field === "message") {
117+
const messageError = validateMessage(value)
118+
if (messageError)
119+
setErrors((prev) => ({ ...prev, message: messageError }))
120+
return
121+
}
122+
}
123+
106124
const validateEmail = (email: string): string | undefined => {
107125
const sanitized = sanitizeInput(email)
108126

@@ -190,6 +208,8 @@ const EnterpriseContactForm = ({ strings }: EnterpriseContactFormProps) => {
190208
placeholder={strings.placeholder.input}
191209
value={formData.email}
192210
onChange={handleInputChange("email")}
211+
onBlur={handleBlur("email")}
212+
hasError={!!errors.email}
193213
disabled={submissionState === "submitting"}
194214
/>
195215
{errors.email && (
@@ -204,6 +224,8 @@ const EnterpriseContactForm = ({ strings }: EnterpriseContactFormProps) => {
204224
placeholder={strings.placeholder.textarea}
205225
value={formData.message}
206226
onChange={handleInputChange("message")}
227+
onBlur={handleBlur("message")}
228+
hasError={!!errors.message}
207229
disabled={submissionState === "submitting"}
208230
className="min-h-[120px]"
209231
/>

0 commit comments

Comments
 (0)