-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
55 lines (46 loc) · 1.73 KB
/
Copy pathscript.js
File metadata and controls
55 lines (46 loc) · 1.73 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
document.addEventListener('DOMContentLoaded', function() {
const form = document.getElementById('signup-form');
const emailInput = document.getElementById('email');
const messageDiv = document.getElementById('form-message');
form.addEventListener('submit', function(e) {
e.preventDefault();
const email = emailInput.value.trim();
// Basic email validation
if (!email || !isValidEmail(email)) {
showMessage('Please enter a valid email address.', 'error');
return;
}
// Submit to Netlify Forms
const formData = new FormData(form);
fetch('/', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams(formData).toString()
})
.then(response => {
if (response.ok) {
showMessage('Thanks! We\'ll notify you when we launch.', 'success');
emailInput.value = '';
} else {
throw new Error('Submission failed');
}
})
.catch(error => {
console.error('Error:', error);
showMessage('Something went wrong. Please try again.', 'error');
});
});
function isValidEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
function showMessage(message, type) {
messageDiv.textContent = message;
messageDiv.className = `form-message ${type}`;
// Clear message after 5 seconds
setTimeout(() => {
messageDiv.textContent = '';
messageDiv.className = 'form-message';
}, 5000);
}
});