-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathContactUs.js
More file actions
209 lines (182 loc) · 5.41 KB
/
Copy pathContactUs.js
File metadata and controls
209 lines (182 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// const onecards = [
// {
// icon: "✈️",
// title: "Travel Inquiries",
// info: "hello@vehigo.com",
// sub: "Response within 2 hours",
// bgClass: "card-blue",
// iconClass: "icon-blue",
// },
// {
// icon: "🌍",
// title: "24/7 Support",
// info: "1800-100-100",
// sub: "Emergency assistance",
// bgClass: "card-green",
// iconClass: "icon-green",
// },
// {
// icon: "📍",
// title: "Visit Our Office",
// info: "Xyz, New Delhi",
// sub: "Mon-Sat: 9:00AM-6:00PM",
// bgClass: "card-purple",
// iconClass: "icon-purple",
// },
// ];
// const cardsContainer = document.getElementById("ContactUscards");
// onecards.forEach((card) => {
// const div = document.createElement("div");
// div.className = `card ${card.bgClass}`;
// div.innerHTML = `
// <div class="icon ${card.iconClass}">${card.icon}</div>
// <div>
// <br>
// <h4 class="title text-light">${card.title}</h4>
// <p class="info text-light">${card.info}</p>
// <p class="sub text-light">${card.sub}</p>
// </div>
// `;
// cardsContainer.appendChild(div);
// });
const form = document.getElementById("form");
const success = document.getElementById("successMessage");
const nameInput = document.querySelector('input[name="name"]');
const emailInput = document.querySelector('input[name="email"]');
const messageInput = document.querySelector('textarea[name="message"]');
function validateName(name) {
const trimmedName = name.trim();
if (trimmedName.length < 3) {
return { valid: false, message: "Name must be at least 3 characters long" };
}
if (!/^[a-zA-Z\s'-]+$/.test(trimmedName)) {
return {
valid: false,
message: "Name can only contain letters, spaces, hyphens and apostrophes",
};
}
return { valid: true };
}
function validateEmail(email) {
const trimmedEmail = email.trim();
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(trimmedEmail)) {
return { valid: false, message: "Please enter a valid email address" };
}
return { valid: true };
}
function validateMessage(message) {
const trimmedMessage = message.trim();
if (trimmedMessage.length < 10) {
return {
valid: false,
message: "Message must be at least 10 characters long",
};
}
if (trimmedMessage.length > 500) {
return { valid: false, message: "Message cannot exceed 500 characters" };
}
return { valid: true };
}
function showError(input, message) {
const formGroup = input.parentElement;
let errorElement = formGroup.querySelector(".error-message");
if (!errorElement) {
errorElement = document.createElement("span");
errorElement.className = "error-message";
formGroup.appendChild(errorElement);
}
errorElement.textContent = message;
input.classList.add("input-error");
input.setAttribute("aria-invalid", "true");
input.setAttribute(
"aria-describedby",
errorElement.id || "error-" + input.name
);
}
function clearError(input) {
const formGroup = input.parentElement;
const errorElement = formGroup.querySelector(".error-message");
if (errorElement) {
errorElement.textContent = "";
}
input.classList.remove("input-error");
input.setAttribute("aria-invalid", "false");
input.removeAttribute("aria-describedby");
}
nameInput.addEventListener("blur", () => {
const validation = validateName(nameInput.value);
if (!validation.valid) {
showError(nameInput, validation.message);
} else {
clearError(nameInput);
}
});
emailInput.addEventListener("blur", () => {
const validation = validateEmail(emailInput.value);
if (!validation.valid) {
showError(emailInput, validation.message);
} else {
clearError(emailInput);
}
});
messageInput.addEventListener("blur", () => {
const validation = validateMessage(messageInput.value);
if (!validation.valid) {
showError(messageInput, validation.message);
} else {
clearError(messageInput);
}
});
nameInput.addEventListener("input", () => {
if (nameInput.classList.contains("input-error")) {
const validation = validateName(nameInput.value);
if (validation.valid) {
clearError(nameInput);
}
}
});
emailInput.addEventListener("input", () => {
if (emailInput.classList.contains("input-error")) {
const validation = validateEmail(emailInput.value);
if (validation.valid) {
clearError(emailInput);
}
}
});
messageInput.addEventListener("input", () => {
if (messageInput.classList.contains("input-error")) {
const validation = validateMessage(messageInput.value);
if (validation.valid) {
clearError(messageInput);
}
}
});
form.addEventListener("submit", (e) => {
e.preventDefault();
const nameValidation = validateName(nameInput.value);
const emailValidation = validateEmail(emailInput.value);
const messageValidation = validateMessage(messageInput.value);
let isValid = true;
if (!nameValidation.valid) {
showError(nameInput, nameValidation.message);
isValid = false;
}
if (!emailValidation.valid) {
showError(emailInput, emailValidation.message);
isValid = false;
}
if (!messageValidation.valid) {
showError(messageInput, messageValidation.message);
isValid = false;
}
if (isValid) {
form.classList.add("hidden");
success.classList.remove("hidden");
form.reset();
setTimeout(() => {
success.classList.add("hidden");
form.classList.remove("hidden");
}, 3000);
}
});