-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontact.html
More file actions
217 lines (193 loc) · 7.48 KB
/
contact.html
File metadata and controls
217 lines (193 loc) · 7.48 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
210
211
212
213
214
215
216
217
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact Us</title>
<style>
.contact-form {
max-width: 600px;
margin: 40px auto;
padding: 20px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input[type="text"],
input[type="email"],
textarea {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 16px;
}
textarea {
height: 150px;
resize: vertical;
}
button {
background-color: #007bff;
color: white;
padding: 12px 30px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #0056b3;
}
.error {
color: red;
font-size: 14px;
margin-top: 5px;
}
.success {
color: green;
padding: 15px;
background-color: #d4edda;
border-radius: 4px;
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="contact-form">
<h1>Contact Us</h1>
<div id="success-message" class="success" style="display: none;">
Thank you for your message! We'll get back to you soon.
</div>
<form id="contactForm" method="POST" action="/api/contact">
<!-- Honeypot field for spam protection -->
<input type="text" name="website" style="display: none;" tabindex="-1" autocomplete="off">
<div class="form-group">
<label for="fullName">Full Name *</label>
<input type="text" id="fullName" name="fullName" required
minlength="2" maxlength="100"
placeholder="Enter your full name">
<div id="fullName-error" class="error"></div>
</div>
<div class="form-group">
<label for="email">Email Address *</label>
<input type="email" id="email" name="email" required
maxlength="254"
placeholder="Enter your email address">
<div id="email-error" class="error"></div>
</div>
<div class="form-group">
<label for="subject">Subject *</label>
<input type="text" id="subject" name="subject" required
minlength="5" maxlength="200"
placeholder="What is this about?">
<div id="subject-error" class="error"></div>
</div>
<div class="form-group">
<label for="body">Message *</label>
<textarea id="body" name="body" required
minlength="10" maxlength="5000"
placeholder="Write your message here..."></textarea>
<div id="body-error" class="error"></div>
</div>
<button type="submit">Send Message</button>
</form>
</div>
<script>
// Client-side form validation
const form = document.getElementById('contactForm');
const successMessage = document.getElementById('success-message');
function validateEmail(email) {
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(email);
}
function showError(fieldId, message) {
document.getElementById(fieldId + '-error').textContent = message;
}
function clearErrors() {
document.querySelectorAll('.error').forEach(el => el.textContent = '');
}
form.addEventListener('submit', async function(e) {
e.preventDefault();
clearErrors();
const fullName = document.getElementById('fullName').value.trim();
const email = document.getElementById('email').value.trim();
const subject = document.getElementById('subject').value.trim();
const body = document.getElementById('body').value.trim();
let isValid = true;
// Validate full name
if (fullName.length < 2) {
showError('fullName', 'Full name must be at least 2 characters');
isValid = false;
} else if (fullName.length > 100) {
showError('fullName', 'Full name must not exceed 100 characters');
isValid = false;
}
// Validate email
if (!validateEmail(email)) {
showError('email', 'Please enter a valid email address');
isValid = false;
}
// Validate subject
if (subject.length < 5) {
showError('subject', 'Subject must be at least 5 characters');
isValid = false;
} else if (subject.length > 200) {
showError('subject', 'Subject must not exceed 200 characters');
isValid = false;
}
// Validate body
if (body.length < 10) {
showError('body', 'Message must be at least 10 characters');
isValid = false;
} else if (body.length > 5000) {
showError('body', 'Message must not exceed 5000 characters');
isValid = false;
}
if (!isValid) return;
// Submit form
const formData = { fullName, email, subject, body };
try {
const response = await fetch('/api/contact', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(formData)
});
if (response.ok) {
successMessage.style.display = 'block';
form.reset();
window.scrollTo(0, 0);
} else {
const error = await response.json();
alert('Error: ' + (error.message || 'Failed to send message'));
}
} catch (error) {
alert('Error: Unable to send message. Please try again later.');
}
});
// Real-time validation feedback
document.getElementById('fullName').addEventListener('blur', function() {
const value = this.value.trim();
if (value.length > 0 && value.length < 2) {
showError('fullName', 'Full name must be at least 2 characters');
} else {
showError('fullName', '');
}
});
document.getElementById('email').addEventListener('blur', function() {
const value = this.value.trim();
if (value.length > 0 && !validateEmail(value)) {
showError('email', 'Please enter a valid email address');
} else {
showError('email', '');
}
});
</script>
</body>
</html>