forked from ANSHIKA-26/WordWise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact_us.js
91 lines (71 loc) · 2.34 KB
/
contact_us.js
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
document.addEventListener("DOMContentLoaded", () => {
const sendEmailButton = document.getElementById("sendEmailButton");
sendEmailButton.addEventListener("click", SendEmail);
});
async function SendEmail(event) {
event.preventDefault();
const Name = document.getElementById("Name").value.trim(); // Corrected ID
const email = document.getElementById("email2").value.trim(); // Corrected ID
const phone = document.getElementById("phone").value.trim();
const message = document.getElementById("message").value.trim();
// Input validation
if (!Name || !email || !phone || !message) {
alert("All fields are required.");
return;
}
if (!validateEmail(email)) {
alert("Please enter a valid email address.");
return;
}
console.log(Name, email, phone, message); // Corrected variable name
if (message.length < 10) {
alert("Message must be at least 10 characters long.");
return;
}
const data = {
Name: Name,
email: email,
phone: phone,
message: message,
};
try {
const response = await fetch("http://127.0.0.1:3000/send-email", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});
const result = await response.json();
if (response.ok) {
showPopup();
document.getElementById("contactForm").reset();
} else {
handleErrorResponse(result);
}
} catch (error) {
console.error("Error sending email:", error);
alert("Error sending email. Please try again later.");
}
}
function validateEmail(email) {
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(String(email).toLowerCase());
}
function showPopup() {
const popup = document.getElementById("popupMessage");
const overlay = document.getElementById("overlay");
overlay.style.display = "block";
popup.style.display = "block";
// Close the popup
document.getElementById("closePopup").addEventListener("click", hidePopup);
// Close when clicking outside
overlay.addEventListener("click", hidePopup);
}
function hidePopup() {
const popup = document.getElementById("popupMessage");
const overlay = document.getElementById("overlay");
popup.style.display = "none";
overlay.style.display = "none";
}
function handleErrorResponse(result) {
alert(`Error: ${result.message || 'An unexpected error occurred.'}`);
}