-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathform.js
More file actions
42 lines (35 loc) · 1.27 KB
/
form.js
File metadata and controls
42 lines (35 loc) · 1.27 KB
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
const form = document.getElementById("interactive-form");
const name = document.getElementById("name");
const email = document.getElementById("email");
const age = document.getElementById("age");
const location = document.getElementById("location");
const nameError = document.getElementById("name-error");
const emailError = document.getElementById("email-error");
const ageError = document.getElementById("age-error");
const locationError = document.getElementById("location-error");
form.addEventListener("submit", function(event) {
event.preventDefault();
// Reset error messages
nameError.textContent = "";
emailError.textContent = "";
ageError.textContent = "";
locationError.textContent = "";
// Validate name
if (!name.value) {
nameError.textContent = "Name is required";
}
// Validate email
if (!email.value) {
emailError.textContent = "Email is required";
} else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email.value)) {
emailError.textContent = "Email is invalid";
}
// Validate age
if (!age.value) {
ageError.textContent = "Age is required";
} else if (age.value < 18) {
ageError.textContent = "You must be at least 18 years old";
}
// Validate location
if (!location.value) {
}