-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscribe.js
43 lines (38 loc) · 1.45 KB
/
subscribe.js
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
function submitForm(event) {
event.preventDefault();
// Reset error messages
document.getElementById('nameError').textContent = '';
document.getElementById('emailError').textContent = '';
document.getElementById('messageError').textContent = '';
// Get form inputs
var nameInput = document.getElementById('name');
var emailInput = document.getElementById('email');
var messageInput = document.getElementById('message');
// Validate inputs
var isValid = true;
if (nameInput.value.trim() === '') {
document.getElementById('nameError').textContent = 'Please enter your name';
isValid = false;
}
if (emailInput.value.trim() === '') {
document.getElementById('emailError').textContent = 'Please enter your email';
isValid = false;
} else if (!validateEmail(emailInput.value)) {
document.getElementById('emailError').textContent = 'Please enter a valid email address';
isValid = false;
}
if (messageInput.value.trim() === '') {
document.getElementById('messageError').textContent = 'Please enter your message';
isValid = false;
}
// Display success message in a pop-up window if form is valid
if (isValid) {
window.alert('Message sent successfully!');
document.getElementById('contactForm').reset();
}
}
// Email validation function
function validateEmail(email) {
var re = /\S+@\S+\.\S+/;
return re.test(email);
}