Skip to content

Commit 33ae55c

Browse files
committed
chore: fixed a bug
1 parent a401265 commit 33ae55c

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/scripts/helpers.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import type { FormFields } from "../types";
22

33
const validateEmail = (email: string) => {
4-
const emailRegExp = /^[\w.!#$%&'*+/=?^`{|}~-]+@[a-z\d-]+(?:\.[a-z\d-]+)*\.[a-z]{2,}$/i; // np. example@gmail.com | muszą być przynajmniej 2 LITERY po gmail
4+
const emailRegExp = /^[\w.!#$%&'*+/=?^`{|}~-]+@[a-z\d-]+(?:\.[a-z\d-]+)*\.[a-z]{2,}$/i; // np. example@gmail.com | there MUST be atleast two letters after @gmail.
55
return email.length > 0 && emailRegExp.test(email);
66
}
77

8+
const validatePhone = (phone: string) => {
9+
const phoneRegExp = /^(\+?\d{2}|00\d{2})?\s?([0-9]{3}\s[0-9]{3}\s[0-9]{3}|[0-9]{9})$/;
10+
return phone.length > 0 && phoneRegExp.test(phone);
11+
}
12+
813
export const validateContactForm = (data: FormFields) => {
914
const array = Object.entries(data);
1015
let valid = true;
@@ -19,8 +24,9 @@ export const validateContactForm = (data: FormFields) => {
1924
}
2025

2126
const isEmailValid = validateEmail(data.email.trim());
27+
const isPhoneValid = validatePhone(data.phone.trim());
2228

23-
if(!isEmailValid){
29+
if(!isEmailValid || !isPhoneValid){
2430
return false;
2531
}
2632

src/sections/ContactForm.astro

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ const t = useTranslations(lang);
3636
<Input
3737
type="tel"
3838
placeholder={t("phone")}
39-
name="tel"
40-
pattern="[0-9]{3} [0-9]{3} [0-9]{3}|[0-9]{9}"
39+
name="phone"
4140
/>
4241
</div>
4342
<div class="formTopSectionInput">
@@ -54,7 +53,9 @@ const t = useTranslations(lang);
5453
<Textarea placeholder={t("content")} name="content" />
5554
</div>
5655
</div>
57-
<span id="errorMessage"></span>
56+
<div class="errorMessageContainer">
57+
<span id="errorMessage"></span>
58+
</div>
5859
<div class="formButtonContainer">
5960
<Button variant="primary" type="submit" id="formSubmitButton">{t("send")}</Button>
6061
</div>
@@ -89,15 +90,17 @@ const t = useTranslations(lang);
8990

9091
const formData = new FormData(form);
9192
const data = Object.fromEntries(formData.entries()) as unknown as FormFields;
93+
console.log(data);
9294
const validation = validateContactForm(data);
9395

9496
if(!validation && errorMessageSpan != null){
9597
errorMessageSpan.innerText = "Please ensure all required fields are filled out correctly before submitting.";
98+
return;
9699
}
97100

98-
const response = await sendTicket(data);
101+
const response = await sendTicket(data);
99102

100-
// jezeli response jest pozytywny to wtedy zmieniamy widok formularza na wyslany
103+
// if response is positive, change the view to "sent form view"
101104
})
102105

103106

@@ -123,9 +126,16 @@ const t = useTranslations(lang);
123126
margin-top: 70px;
124127
}
125128

129+
.errorMessageContainer{
130+
width: 100%;
131+
display: flex;
132+
align-items: center;
133+
justify-content: center;
134+
}
135+
126136
#errorMessage{
127137
color: red;
128-
width: calc(100% - 40px);
138+
width: 100%;
129139
padding: 0px 20px;
130140
}
131141

0 commit comments

Comments
 (0)