-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
80 lines (67 loc) · 2.45 KB
/
index.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
68
69
70
71
72
73
74
75
76
77
78
79
80
// JavaScript Fundamentals: Form Validation and Sending an Email
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('contactForm');
const messageElement = document.getElementById('formMessage');
form.addEventListener('submit', async (e) => {
e.preventDefault();
// Collect form data
const name = document.getElementById('name').value.trim();
const email = document.getElementById('email').value.trim();
const message = document.getElementById('message').value.trim();
// Basic validation
if (!name || !email || !message) {
showMessage('Please fill in all fields.', 'error');
return;
}
if (!isValidEmail(email)) {
showMessage('Please enter a valid email address.', 'error');
return;
}
// Use EmailJS for sending emails
try {
const response = await sendEmail(name, email, message);
if (response.ok) {
showMessage('Your message was sent successfully!', 'success');
form.reset();
} else {
showMessage('Failed to send your message. Please try again.', 'error');
}
} catch (error) {
showMessage('An error occurred. Please try again later.', 'error');
}
});
// Helper Functions
function showMessage(message, type) {
messageElement.textContent = message;
messageElement.style.color = type === 'success' ? 'green' : 'red';
}
function isValidEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
async function sendEmail(name, email, message) {
const serviceID = 'service_41hq4nq';
const templateID = 'your_template_id';
const userID = 'your_user_id';
return await fetch('https://api.emailjs.com/api/v1.0/email/send', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
service_id: serviceID,
template_id: templateID,
user_id: userID,
template_params: { name, email, message },
}),
});
}
});
document.addEventListener('DOMContentLoaded', () => {
const slides = document.querySelector('.slides');
const slideCount = document.querySelectorAll('.slide').length;
let currentIndex = 0;
// Automatically move to the next slide every 3 seconds
setInterval(() => {
currentIndex = (currentIndex + 1) % slideCount; // Loop back to the first slide
slides.style.transform = `translateX(-${currentIndex * 100}%)`;
}, 3000);
});