-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform.js
More file actions
30 lines (24 loc) · 862 Bytes
/
form.js
File metadata and controls
30 lines (24 loc) · 862 Bytes
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
const contactForm = document.getElementById('contactForm');
contactForm.addEventListener('submit', (event) => {
event.preventDefault(); // Prevent default form submission
const name = document.getElementById('name').value;
const email = document.getElementById('email').value;
const message = document.getElementById('message').value;
// Basic validation
if (!name || !email || !message) {
alert('Please fill in all required fields.');
return;
}
// Additional validation (e.g., email format)
if (!isValidEmail(email)) {
alert('Invalid email address.');
return;
}
// Submit form data (e.g., send to a server)
console.log('Form submitted:', { name, email, message });
});
function isValidEmail(email) {
// Simple email validation
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}