forked from ANSHIKA-26/WordWise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact_us.js
142 lines (119 loc) · 3.62 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
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
document.addEventListener("DOMContentLoaded", () => {
const sendEmailButton = document.getElementById("sendEmailButton");
sendEmailButton.addEventListener("click", SendEmail);
});
const trustedDomains = [
'gmail.com',
'outlook.com',
'yahoo.com',
'protonmail.com',
'icloud.com',
'tutanota.com',
'hotmail.com',
'live.com',
'mail.com',
'zoho.com',
'gmx.com',
'aol.com',
'fastmail.com',
'yandex.com',
'*.edu',
'*.ac.uk',
'*.edu.in',
'*.edu.au',
'examplecompany.com',
'mailfence.com',
'posteo.de',
'runbox.com'
];
// Email validation function to check format and domain
function ValidateTrustEmail(email) {
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; // Basic email format validation
const domain = email.split('@')[1];
return (
emailPattern.test(email) &&
trustedDomains.some((trusted) =>
trusted.includes('*') ? domain.endsWith(trusted.slice(1)) : domain === trusted
)
);
}
async function SendEmail(event) {
event.preventDefault();
const Name = document.getElementById("Name").value.trim();
const email = document.getElementById("email2").value.trim();
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 (!ValidateTrustEmail(email)) {
alert("Please enter a valid email address from a trusted provider.");
return;
}
if (message.length < 10) {
alert("Message must be at least 10 characters long.");
return;
}
// If validation passes
alert('Form submitted successfully!');
const data = { Name, email, phone, 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);
}
}
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.'}`);
}
function validateForm() {
const phonePattern = /^\d{10}$/; //for 10-digit phone numbers
// Validate phone number
if (!phonePattern.test(phone)) {
alert('Please enter a valid phone number (10 digits).');
return; // Stop further execution
}
}
function restrictInputToNumbers(event) {
const key = event.key;
if (!/^\d$/.test(key) && key !== "Backspace" && key !== "Delete") {
event.preventDefault(); // Prevent input if it's not a digit
}
}
document.addEventListener('DOMContentLoaded', () => {
const phoneInput = document.getElementById('phone');
phoneInput.addEventListener('keydown', restrictInputToNumbers);
});