Repository navigation
Expand file tree
/
Copy pathcontact.js
More file actions
37 lines (34 loc) · 1.71 KB
/
Copy pathcontact.js
File metadata and controls
37 lines (34 loc) · 1.71 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
// Formspree endpoint ID for this form (the part after /f/ in the Formspree form URL).
const FORMSPREE_ID = 'xeaedqdp';
(function () {
var f = document.getElementById('cf');
if (!f) return;
var st = f.querySelector('.cf-status');
var b = f.querySelector('button[type=submit]');
var label = b.textContent;
function show(cls, msg) { st.className = 'cf-status' + (cls ? ' ' + cls : ''); st.textContent = msg; }
f.addEventListener('submit', function (e) {
e.preventDefault();
var name = f.elements.name.value.trim();
var email = f.elements.email.value.trim();
var msg = f.elements.message.value.trim();
if (!name) return show('err', 'Please enter your name.');
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) return show('err', 'Please enter a valid email address.');
if (msg.length < 2) return show('err', 'Please enter a message.');
if (!FORMSPREE_ID || FORMSPREE_ID === 'REPLACE_ME') {
return show('err', 'Sorry, the contact form isn\u2019t switched on yet. Please try again soon.');
}
b.disabled = true; b.textContent = 'Sending...'; show('', '');
fetch('https://formspree.io/f/' + FORMSPREE_ID, {
method: 'POST',
headers: { 'Accept': 'application/json' },
body: new FormData(f)
})
.then(function (r) {
if (r.ok) { f.reset(); show('ok', 'Thank you! Your message has been sent. We\u2019ll reply by email soon.'); }
else { show('err', 'Sorry, something went wrong sending your message. Please try again.'); }
})
.catch(function () { show('err', 'Sorry, we couldn\u2019t reach the server. Please check your connection and try again.'); })
.finally(function () { b.disabled = false; b.textContent = label; });
});
})();