forked from GarimaSingh0109/WasteManagment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
100 lines (82 loc) · 3.36 KB
/
script.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const imageInput = document.getElementById('image-input');
const uploadButton = document.getElementById('upload-button');
const classificationResult = document.getElementById('classification-result');
const disposalInformation = document.getElementById('disposal-information');
uploadButton.addEventListener('click', () => {
const image = imageInput.files[0];
const formData = new FormData();
formData.append('image', image);
fetch('/classify', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
classificationResult.textContent = data.classification;
disposalInformation.textContent = data.disposalInformation;
})
.catch(error => console.error(error));
});
// FAQ functionality
const faqItems = document.querySelectorAll('.faq-item h3');
faqItems.forEach(item => {
item.addEventListener('click', function() {
const parent = this.parentElement;
parent.classList.toggle('active');
});
});
// Optional: Check the initial mode on page load
if (localStorage.getItem('dark-mode') === 'enabled') {
enableDarkMode();
}
// Feedback form validation
const feedbackForm = document.getElementById('feedback-form');
const feedbackName = feedbackForm.querySelector('input[type="text"]');
const feedbackEmail = feedbackForm.querySelector('input[type="email"]');
const feedbackMessage = feedbackForm.querySelector('textarea');
feedbackForm.addEventListener('submit', (event) => {
event.preventDefault(); // Prevent the form from submitting
const name = feedbackName.value.trim();
const email = feedbackEmail.value.trim();
const message = feedbackMessage.value.trim();
let valid = true;
// Clear previous error messages
feedbackName.setCustomValidity('');
feedbackEmail.setCustomValidity('');
// Name validation
if (name.length < 2) {
feedbackName.setCustomValidity('Name must be at least 2 characters long.');
valid = false;
}
// Email validation
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailPattern.test(email)) {
feedbackEmail.setCustomValidity('Please enter a valid email address.');
valid = false;
}
// Message validation
if (message.length < 10) {
alert('Message must be at least 10 characters long.');
valid = false;
}
if (valid) {
alert('Feedback submitted successfully!');
feedbackForm.reset(); // Reset the form after successful submission
} else {
feedbackName.reportValidity();
feedbackEmail.reportValidity();
}
});
// Newsletter form validation
const newsletterForm = document.getElementById('newsletter-form');
const newsletterEmail = newsletterForm.querySelector('input[type="email"]');
const newsletterErrorMessage = document.getElementById('newsletter-error-message');
newsletterForm.addEventListener('submit', (event) => {
event.preventDefault(); // Prevent the form from submitting
const email = newsletterEmail.value.trim();
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
// Clear previous error message
newsletterErrorMessage.textContent = '';
if (!emailPattern.test(email)) {
newsletterErrorMessage.textContent = 'Please enter'};
})