-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalumniValidation.html
104 lines (85 loc) · 3.12 KB
/
alumniValidation.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Alumni Form</title>
<style>
form {
width: 300px;
margin: 20px auto;
padding: 15px;
border: 1px solid #ccc;
border-radius: 8px;
}
input {
width: 100%;
margin: 10px 0;
padding: 8px;
}
.error {
color: red;
font-size: 12px;
}
</style>
</head>
<body>
<h2 style="text-align: center;">Alumni Form</h2>
<form id="alumniForm" onsubmit="return validateForm()">
<input type="text" id="name" placeholder="Name">
<span class="error" id="nameError"></span>
<input type="text" id="address" placeholder="Address">
<span class="error" id="addressError"></span>
<input type="date" id="dob" placeholder="Date of Birth">
<span class="error" id="dobError"></span>
<input type="text" id="email" placeholder="Email">
<span class="error" id="emailError"></span>
<button type="submit">Submit</button>
</form>
<script>
function validateForm() {
const name = document.getElementById("name").value.trim();
const address = document.getElementById("address").value.trim();
const dob = document.getElementById("dob").value;
const email = document.getElementById("email").value.trim();
const nameError = document.getElementById("nameError");
const addressError = document.getElementById("addressError");
const dobError = document.getElementById("dobError");
const emailError = document.getElementById("emailError");
// Reset errors
nameError.textContent = "";
addressError.textContent = "";
dobError.textContent = "";
emailError.textContent = "";
let isValid = true;
// Check if all fields are filled
if (name === "") {
nameError.textContent = "Name is required.";
isValid = false;
}
if (address === "") {
addressError.textContent = "Address is required.";
isValid = false;
}
if (dob === "") {
dobError.textContent = "Date of Birth is required.";
isValid = false;
} else {
const birthYear = new Date(dob).getFullYear();
const currentYear = new Date().getFullYear();
const age = currentYear - birthYear;
if (age < 22) {
dobError.textContent = "You must be at least 22 years old.";
isValid = false;
}
}
// Validate email
if (email === "" || !email.includes("@") || !email.includes(".")) {
emailError.textContent = "Enter a valid email address.";
isValid = false;
}
return isValid;
}
</script>
</body>
</html>