-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrievance.js
68 lines (58 loc) · 2.27 KB
/
grievance.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
// main.js
const form = document.getElementById('grievanceForm');
form.addEventListener('submit', (e) => {
e.preventDefault();
// Retrieve form data
const name = document.getElementById('name').value;
const email = document.getElementById('email').value;
const mobile = document.getElementById('mobile').value;
const complaintType = document.getElementById('complaintType').value;
const society = document.getElementById('society').value;
const feedback = document.getElementById('feedback').value;
const phoneNumberRegex = /^\d{10}$/; // Regex for 10-digit phone number
const isValidPhoneNumber = phoneNumberRegex.test(mobile);
// Check for empty fields
if (!name || !email || !mobile || !complaintType || !society || !feedback) {
const errorMessage = document.createElement('p');
errorMessage.className = 'error-message';
errorMessage.innerText = 'Please fill in all fields.';
form.appendChild(errorMessage);
return;
}
else if (!isValidPhoneNumber) {
const errorMessage = document.createElement('p');
errorMessage.className = 'error-message';
errorMessage.innerText = 'Please enter a valid 10-digit phone number.';
form.appendChild(errorMessage);
return;
}
else if (feedback.trim().split(/\s+/).length < 20) {
const errorMessage = document.createElement('p');
errorMessage.className = 'error-message';
errorMessage.innerText = 'Please provide at least 20 words in the Complaint/Feedback section.';
form.appendChild(errorMessage);
return;
}
// Remove any existing error message
const errorMessage = document.querySelector('.error-message');
if (errorMessage) {
errorMessage.remove();
}
// Create an object to store form data
const formData = {
name: name,
email: email,
mobile: mobile,
complaintType: complaintType,
society: society,
feedback: feedback
};
// Retrieve existing data from Local Storage or initialize an empty array
const existingData = JSON.parse(localStorage.getItem('formData')) || [];
// Add the new form data to the existing array
existingData.push(formData);
// Store the updated array in Local Storage
localStorage.setItem('formData', JSON.stringify(existingData));
// Redirect to the display.html page
window.location.href = 'display.html';
});