-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgr.js
80 lines (70 loc) · 2.25 KB
/
gr.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
function sendEmail(event) {
event.preventDefault();
const name = document.getElementById("name").value.trim();
const email = document.getElementById("email").value.trim();
const phone = document.getElementById("phone").value.trim();
const message = document.getElementById("message").value.trim();
// Validate form fields
const errors = [];
if (name === "") {
errors.push("Name is required");
}
if (email === "") {
errors.push("Email is required");
} else if (!isValidEmail(email)) {
errors.push("Email is invalid");
}
if (phone === "") {
errors.push("Phone is required");
} else if (!isValidPhone(phone)) {
errors.push("Phone is invalid");
}
if (message === "") {
errors.push("Message is required");
}
// Display errors or send email
if (errors.length > 0) {
// Display errors
const errorDiv = document.createElement("div");
errorDiv.classList.add("error");
const errorMessage = document.createElement("p");
errorMessage.innerText = "Please fix the following errors:";
errorDiv.appendChild(errorMessage);
const errorList = document.createElement("li");
errors.forEach((error) => {
const errorItem = document.createElement("li");
errorItem.innerText = error;
errorList.appendChild(errorItem);
});
const form = document.querySelector("form");
form.appendChild(errorDiv);
errorDiv.appendChild(errorList);
return;
}
// Create email body
const body = `
Name: ${name}
Email: ${email}
Phone: ${phone}
Message: ${message}
`;
// Send email
const subject = "Grievances Form Submission";
const mailto = "mailto:[email protected]?subject=" + encodeURIComponent(subject) + "&body=" + encodeURIComponent(body);
window.location.href = mailto;
// Reset form
document.getElementById("name").value = "";
document.getElementById("email").value = "";
document.getElementById("phone").value = "";
document.getElementById("message").value = "";
}
function isValidEmail(email) {
// Regular expression for email validation
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
function isValidPhone(phone) {
// Regular expression for phone validation
const phoneRegex = /^\d{10}$/;
return phoneRegex.test(phone);
}