-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript-contact.js
61 lines (54 loc) · 2.15 KB
/
script-contact.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
function isEmail(email) {
let regex = /[a-zA-Z\d]+@[a-zA-Z\d]+\.[a-zA-Z\d]+/;
return regex.test(email);
}
function submit() {
let submitButton = document.getElementById("submit");
submitButton.addEventListener("click", function validate(event){
event.preventDefault();
let firstName = document.getElementById("fname").value.trim();
let lastName = document.getElementById("lname").value.trim();
let email = document.getElementById("email").value.trim();
let returning = document.querySelectorAll('input[name="returning"]');
let returnChosen = false;
for (let i = 0; i < returning.length; i++) {
if (returning[i].checked) {
returnChosen = true;
break;
}
}
let feedback = document.querySelectorAll('input[name="liked_website"]');
let feedbackChosen = false;
for (let i = 0; i < feedback.length; i++) {
if (feedback[i].checked) {
feedbackChosen = true;
break;
}
}
let message = document.getElementById("error-msg");
if (firstName === "") {
message.innerHTML = "Please enter a first name";
message.style.color = "red";
} else if (lastName === "") {
message.innerHTML = "Please enter a last name";
message.style.color = "red";
} else if (email === "") {
message.innerHTML = "Please enter your email";
message.style.color = "red";
} else if (!isEmail(email)) {
message.innerHTML = "Please enter a valid email";
message.style.color = "red";
} else if (!returnChosen) {
message.innerHTML = "Please answer if you are returning";
message.style.color = "red";
} else if (!feedbackChosen) {
message.innerHTML = "Please choose your feedback";
message.style.color = "red";
} else {
message.innerHTML = "Success!";
message.style.color = "green";
alert("Thank you! Your response has been received.");
}
});
}
submit()