-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
76 lines (65 loc) · 2.83 KB
/
script.js
File metadata and controls
76 lines (65 loc) · 2.83 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
function toggleMenu(evt) {
// Toggle navigation menu visibility
let nav = document.querySelector("#nav-list");
if (nav.style.display === "block") {
evt.target.innerText = "=";
nav.style.display = "none";
nav.style.transition = "opacity 0.1s ease-out";
nav.style.opacity = 0;
} else {
evt.target.innerText = "x";
nav.style.display = "block";
nav.style.transition = "opacity 0.1s ease-in";
nav.style.opacity = 1;
}
}
document.addEventListener("DOMContentLoaded", function() {
var contactForm = document.getElementById("contactForm");
if (contactForm) {
contactForm.addEventListener("submit", function(event) {
// prevent default form submission
event.preventDefault();
// collect form data
const firstName = document.querySelector("#fname")?.value || "";
const lastName = document.querySelector("#lname")?.value || "";
const email = document.querySelector("#email")?.value || "";
const message = document.querySelector("#message")?.value || "";
const phone = document.querySelector("#phone")?.value || "";
const errorMsg = document.querySelector(".error");
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
// validate required fields
if (firstName === "" && lastName === "" && email === "") {
errorMsg.innerText = "Please enter required fields.";
return;
}
// validate email format
if (!emailRegex.test(email)) {
errorMsg.innerText = "Please enter a valid email address.";
return;
}
// Submit form
errorMsg.innerText = ""; // clear any previous error messages
// Build the mailto form
const subject = encodeURIComponent(`Contact Form Submission from ${firstName} ${lastName}`);
const body = encodeURIComponent(
`Name: ${firstName} ${lastName} \r\n` +
`Email: ${email} \r\n` +
`Phone: ${phone} \r\n` +
`Message:${message}`
);
const recipient = 'milton.cruz@batestech.edu';
const mailtoLink = `mailto:${recipient}?subject=${subject}&body=${body}`;
// Error handling for mailto
try {
// Open the mailto link
window.location.href = mailtoLink;
} catch (error) {
// Fallback in case mailto fails
window.open(mailtoLink, '_blank');
errorMsg.innerText = "Could not open email client. Please send your message manually to " + recipient;
}
// reset form
contactForm.reset();
});
}
});