forked from PriyaGhosal/BuddyTrail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact.js
67 lines (55 loc) · 2.34 KB
/
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
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
55
56
57
58
59
60
61
62
63
64
65
66
67
document.getElementById('contactForm').addEventListener('submit', function(e) {
e.preventDefault();
// Basic form validation
let fullname = document.getElementById('fullname').value;
let email = document.getElementById('email').value;
let phone = document.getElementById('phone').value;
let message = document.getElementById('message').value;
// Error elements
let nameError = document.getElementById('nameError');
let emailError = document.getElementById('emailError');
let phoneError = document.getElementById('phoneError');
let messageError = document.getElementById('messageError');
// Clear previous error messages
nameError.textContent = '';
emailError.textContent = '';
phoneError.textContent = '';
messageError.textContent = '';
let hasError = false;
// Validate name (must be more than 1 character and contain letters only)
if (fullname.length < 2 || !/^[a-zA-Z\s]+$/.test(fullname)) {
nameError.textContent = 'Name must be at least 2 characters long and contain only letters.';
nameError.style.display = 'block';
hasError = true;
}
// Validate email format
if (!isValidEmail(email)) {
emailError.textContent = 'Please enter a valid email address.';
emailError.style.display = 'block';
hasError = true;
}
if (!isValidPhone(phone)) {
phoneError.textContent = 'Please enter a valid phone number (e.g., +1234567890).';
phoneError.style.display = 'block';
hasError = true;
}
// Validate message (must be more than 1 character)
if (message.length < 2) {
messageError.textContent = 'Message must be at least 2 characters long.';
messageError.style.display = 'block';
hasError = true;
}
// If there are no errors, proceed with form submission
if (!hasError) {
// If validation passes, you can submit the form data to your server here
console.log('Form submitted:', { fullname, email, phone, message });
alert('Thank you for your message. We will get back to you soon!');
this.reset();
}
});
function isValidEmail(email) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
}
function isValidPhone(phone) {
return /^\+?[1-9]\d{1,14}$/.test(phone); // e.g., +1234567890
}