исправила баг имени в контактной форме

This commit is contained in:
xomimishka committed 2025-04-22 15:51:06 +03:00
1 parent 2a0f437744
commit dd5d54a4e4
2 files changed
+16 -3

No files matched your search

+15 -2
View File
@@ -15,7 +15,11 @@ import {
} from "@/widgets/ContactForm/variables";
const formSchema = z.object({
name: z.string().min(1, "Обязательное поле"),
name: z
.string()
.trim()
.min(1, "Обязательное поле")
.regex(/^[a-zA-Zа-яА-ЯёЁ\s-]+$/, "Имя может содержать только буквы и пробелы"),
email: z.string().email("Некорректный email. Пример 123@sfedu.ru"),
phone: z.string()
.min(1, "Обязательное поле")
@@ -145,6 +149,13 @@ function ContactForm() {
});
};
const handleNameChange = (e: React.ChangeEvent<HTMLInputElement>) => {
// Разрешены только буквы (латиница и кириллица), пробелы и дефисы
const rawValue = e.target.value;
const cleanedValue = rawValue.replace(/[^a-zA-Zа-яА-ЯёЁ\s-]/g, '');
setValue("name", cleanedValue, { shouldValidate: true });
};
return (
<Form
method="post"
@@ -158,7 +169,9 @@ function ContactForm() {
name="name"
placeholder="Имя"
disabled={isSubmitting || isPending}
register={register("name")}
register={register("name", {
onChange: handleNameChange,
})}
error={errors.name?.message}
/>
<Field
+1 -1
View File
@@ -41,7 +41,7 @@ const PhotoGallery = ({ images }: PhotoGalleryProps) => {
</div>
<div className="btn">
Показать еще
Загрузить ещё
</div>
</div>
);