-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact.js
More file actions
124 lines (105 loc) · 4.49 KB
/
Copy pathcontact.js
File metadata and controls
124 lines (105 loc) · 4.49 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
import { isValidEmail } from "/utils/validation.js";
document.addEventListener("DOMContentLoaded", function () {
const form = document.getElementById("contactForm");
const statusMessage = document.getElementById("statusMessage");
const formFailureHelp = document.getElementById("formFailureHelp");
// Check URL parameters for successful form submission
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.get("success") === "true") {
statusMessage.textContent =
"Thank you! Your message has been sent successfully.";
statusMessage.className = "form-status success";
statusMessage.style.display = "block";
form.reset();
// Scroll to status message
statusMessage.scrollIntoView({
behavior: "smooth",
block: "nearest",
});
}
// Basic client-side validation
form.addEventListener("submit", function (e) {
// Honeypot: if a bot filled the hidden "website" field, silently abort.
// We intentionally don't show an error — let the bot think it worked.
const honeypot = document.getElementById("website");
if (honeypot && honeypot.value.trim() !== "") {
e.preventDefault();
return false;
}
// Reset previous errors
resetErrors();
// Validate form
if (!validateForm()) {
e.preventDefault(); // Prevent form submission
return false;
}
// Handle anonymous email
const emailInput = document.getElementById("email");
if (!emailInput.value.trim()) {
emailInput.value = "anonymous@organize.directory";
}
// Show help div after a delay in case submission is slow
setTimeout(() => {
formFailureHelp.style.display = "block";
}, 15000);
return true; // Allow form submission
});
// Form validation function
function validateForm() {
let isValid = true;
// Validate name
const nameInput = document.getElementById("name");
const nameError = document.getElementById("nameError");
if (!nameInput.value.trim()) {
nameInput.classList.add("error");
nameError.style.display = "block";
isValid = false;
}
// Validate email if provided
const emailInput = document.getElementById("email");
const emailError = document.getElementById("emailError");
if (emailInput.value.trim() && !isValidEmail(emailInput.value)) {
emailInput.classList.add("error");
emailError.style.display = "block";
isValid = false;
}
// Validate inquiry
const inquiryInput = document.getElementById("inquiry");
const inquiryError = document.getElementById("inquiryError");
if (!inquiryInput.value) {
inquiryInput.classList.add("error");
inquiryError.style.display = "block";
isValid = false;
}
// Validate message
const messageInput = document.getElementById("message");
const messageError = document.getElementById("messageError");
if (!messageInput.value.trim()) {
messageInput.classList.add("error");
messageError.style.display = "block";
isValid = false;
}
// If not valid, scroll to first error
if (!isValid) {
document
.querySelector(".error")
.scrollIntoView({ behavior: "smooth", block: "center" });
}
return isValid;
}
// Reset error messages
function resetErrors() {
const errorInputs = document.querySelectorAll(".error");
const errorTexts = document.querySelectorAll(".error-text");
errorInputs.forEach((input) => input.classList.remove("error"));
errorTexts.forEach((text) => (text.style.display = "none"));
}
// Input event listeners to clear errors on typing
document.querySelectorAll("input, select, textarea").forEach((el) => {
el.addEventListener("input", function () {
this.classList.remove("error");
const errorEl = document.getElementById(this.id + "Error");
if (errorEl) errorEl.style.display = "none";
});
});
});