Contact form errors fix

This commit is contained in:
2024-11-19 19:29:03 +00:00
parent 0a22799555
commit c0b1550609
4 changed files with 28 additions and 5 deletions
+1 -1
View File
@@ -6,6 +6,6 @@ COPY . .
RUN npm install
RUN npm run build
RUN npm install -g
RUN npm install -g serve
CMD ["serve", "-s", "dist"]
+3 -1
View File
@@ -170,7 +170,9 @@
"success": "Your message has been sent successfully!",
"error": "An error occurred while sending your message. Please try again later.",
"empty": "Please fill out all the fields before sending the message.",
"invalidCountryCode": "Please enter a valid country code."
"invalidCountryCode": "Please enter a valid country code.",
"invalidEmail": "Please enter a valid email address.",
"invalidPhone": "Please enter a valid phone number."
}
},
"footer": {
+3 -1
View File
@@ -170,7 +170,9 @@
"success": "A tua mensagem foi enviada com sucesso!",
"error": "Ocorreu um erro ao enviar a tua mensagem. Por favor, tenta novamente mais tarde.",
"empty": "Por favor, preenche todos os campos antes de enviar a mensagem.",
"invalidCountryCode": "Por favor, preenche um código de país válido."
"invalidCountryCode": "Por favor, preenche um código de país válido.",
"invalidEmail": "Por favor, preenche um endereço de email válido.",
"invalidPhone": "Por favor, preenche um número de telefone válido."
}
},
"footer": {
+21 -2
View File
@@ -5,6 +5,7 @@ import "react-toastify/dist/ReactToastify.css";
import { useTranslation } from "react-i18next";
import PhoneInput, {
formatPhoneNumber,
isValidPhoneNumber,
parsePhoneNumber,
} from "react-phone-number-input";
import "react-phone-number-input/style.css";
@@ -26,6 +27,15 @@ const ContactForm = () => {
toast.warning(t("contact.toasts.empty"));
};
const validateEmail = (email: string) => {
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(email);
};
const invalidEmail = () => {
toast.error(t("contact.toasts.invalidEmail"));
};
const invalidCountryCode = () => {
toast.error(t("contact.toasts.invalidCountryCode"));
};
@@ -40,8 +50,17 @@ const ContactForm = () => {
);
if (isEmpty || !phone) {
// Check if phone is also empty
emptyForm();
if (!phone || !isValidPhoneNumber(phone)) {
toast.error(t("contact.toasts.invalidPhone"));
} else {
emptyForm();
}
return;
}
const email = e.currentTarget.user_email.value;
if (!validateEmail(email)) {
invalidEmail();
return;
}