-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeedback.js
143 lines (113 loc) · 4.19 KB
/
feedback.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
function isChecked(name) {
const inputs = document.getElementsByName(name);
for (let i = 0; i < inputs.length; i++) {
if (inputs[i].checked) {
return true;
}
}
return false;
}
function getCheckedValue(name) {
const inputs = document.getElementsByName(name);
for (let i = 0; i < inputs.length; i++) {
if (inputs[i].checked) {
return inputs[i].value;
}
}
return '';
}
function displayError(id, val) {
const errorElement = document.getElementById(id);
if (val) {
errorElement.style.display = 'block';
}
else {
errorElement.style.display = 'none';
}
}
function validateEmail(email) {
const re = /\S+@\S+\.\S+/;
return re.test(email);
}
function validateFields() {
const errors = [];
const name = document.getElementById('name').value.trim();
if (!name) {
displayError('nameError', true);
errors.push('This field is required!');
}
else {
displayError('nameError', false);
}
const email = document.getElementById('email').value.trim();
if (!validateEmail(email)) {
displayError('emailError', true);
errors.push('Please enter a valid email address');
}
else {
displayError('emailError', false);
}
if (!isChecked('firstTime')) {
displayError('firstTimeError', true);
errors.push('Please select one of the given options');
}
else {
displayError('firstTimeError', false);
}
if (!isChecked('informative')) {
displayError('informativeError', true);
errors.push('Please select one of the given options');
}
else {
displayError('informativeError', false);
}
if (!isChecked('rating')) {
displayError('ratingError', true);
errors.push('Please rate your experience');
}
else {
displayError('ratingError', false);
}
return errors.length === 0;
}
function showPreview() {
const previewInfo = document.getElementById('previewInfo');
previewInfo.innerHTML = '';
previewInfo.innerHTML += '<p><strong>Name:</strong> ' + document.getElementById('name').value.trim() + '</p>';
previewInfo.innerHTML += '<p><strong>Email:</strong> ' + document.getElementById('email').value.trim() + '</p>';
previewInfo.innerHTML += '<p><strong>First Time Visiting:</strong> ' + getCheckedValue('firstTime') + '</p>';
previewInfo.innerHTML += '<p><strong>Informative and Easy to Navigate:</strong> ' + getCheckedValue('informative') + '</p>';
previewInfo.innerHTML += '<p><strong>Improvements:</strong> ' + document.getElementById('improvements').value.trim() + '</p>';
previewInfo.innerHTML += '<p><strong>Rating:</strong> ' + getCheckedValue('rating') + '</p>';
previewInfo.innerHTML += '<p><strong>Receive Updates:</strong> ' + document.getElementById('updates').value + '</p>';
previewInfo.innerHTML += '<p><strong>Additional Questions/Requests:</strong> ' + document.getElementById('additional').value.trim() + '</p>';
document.getElementById('feedbackForm').style.display = 'none';
document.getElementById('preview').style.display = 'block';
}
function closePreview() {
document.getElementById('feedbackForm').style.display = 'block';
document.getElementById('preview').style.display = 'none';
}
function validateAndPreview() {
if (validateFields()) {
showPreview();
}
else {
alert('Please complete all required fields before previewing')
}
}
function finalSubmit() {
document.getElementById('feedbackForm').reset();
document.getElementById('feedbackForm').style.display = 'none';
document.getElementById('confirmationMessage').style.display = 'block';
document.getElementById('preview').style.display = 'none';
}
document.getElementById('feedbackForm').addEventListener('submit', function(event) {
event.preventDefault();
if (!validateFields()) {
alert('Please complete all required fields before submitting');
}
else {
finalSubmit();
}
});