forked from iamrahulmahato/master-web-development
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact.js
26 lines (21 loc) · 913 Bytes
/
contact.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
document.getElementById('contactForm').addEventListener('submit', function(event) {
event.preventDefault();
const name = document.getElementById('name').value;
const email = document.getElementById('email').value;
const message = document.getElementById('message').value;
const formMessage = document.getElementById('formMessage');
if (name === '' || email === '' || message === '') {
formMessage.textContent = 'Please fill in all fields.';
return;
}
if (!validateEmail(email)) {
formMessage.textContent = 'Please enter a valid email address.';
return;
}
formMessage.textContent = 'Thank you for contacting us, ' + name + '! We will get back to you soon.';
formMessage.style.color = 'green';
});
function validateEmail(email) {
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(email);
}