-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.js
More file actions
145 lines (121 loc) · 5.05 KB
/
validation.js
File metadata and controls
145 lines (121 loc) · 5.05 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
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
144
145
document.addEventListener('DOMContentLoaded', function() {
const form = document.getElementById('registrationForm');
const studentName = document.getElementById('studentName');
const rollNumber = document.getElementById('rollNumber');
const email = document.getElementById('email');
const department = document.getElementById('department');
const eventName = document.getElementById('eventName');
const studentNameError = document.getElementById('studentNameError');
const rollNumberError = document.getElementById('rollNumberError');
const emailError = document.getElementById('emailError');
const departmentError = document.getElementById('departmentError');
const eventNameError = document.getElementById('eventNameError');
const emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
function clearError(field, errorElement) {
field.classList.remove('invalid');
errorElement.textContent = '';
}
function showError(field, errorElement, message) {
field.classList.add('invalid');
errorElement.textContent = message;
}
function validateStudentName() {
const value = studentName.value.trim();
if (value === '') {
showError(studentName, studentNameError, 'Student name is required');
return false;
} else if (value.length < 3) {
showError(studentName, studentNameError, 'Name must be at least 3 characters long');
return false;
} else if (!/^[a-zA-Z\s]+$/.test(value)) {
showError(studentName, studentNameError, 'Name should contain only letters and spaces');
return false;
} else {
clearError(studentName, studentNameError);
return true;
}
}
function validateRollNumber() {
const value = rollNumber.value.trim();
if (value === '') {
showError(rollNumber, rollNumberError, 'Roll number is required');
return false;
} else if (value.length < 3) {
showError(rollNumber, rollNumberError, 'Roll number must be at least 3 characters long');
return false;
} else {
clearError(rollNumber, rollNumberError);
return true;
}
}
function validateEmail() {
const value = email.value.trim();
if (value === '') {
showError(email, emailError, 'Email address is required');
return false;
} else if (!emailPattern.test(value)) {
showError(email, emailError, 'Please enter a valid email address (e.g., user@example.com)');
return false;
} else {
clearError(email, emailError);
return true;
}
}
function validateDepartment() {
const value = department.value;
if (value === '') {
showError(department, departmentError, 'Please select your department');
return false;
} else {
clearError(department, departmentError);
return true;
}
}
function validateEventName() {
const value = eventName.value;
if (value === '') {
showError(eventName, eventNameError, 'Please select an event to register');
return false;
} else {
clearError(eventName, eventNameError);
return true;
}
}
studentName.addEventListener('blur', validateStudentName);
rollNumber.addEventListener('blur', validateRollNumber);
email.addEventListener('blur', validateEmail);
department.addEventListener('change', validateDepartment);
eventName.addEventListener('change', validateEventName);
studentName.addEventListener('input', function() {
if (studentName.classList.contains('invalid')) {
validateStudentName();
}
});
rollNumber.addEventListener('input', function() {
if (rollNumber.classList.contains('invalid')) {
validateRollNumber();
}
});
email.addEventListener('input', function() {
if (email.classList.contains('invalid')) {
validateEmail();
}
});
form.addEventListener('submit', function(event) {
const isNameValid = validateStudentName();
const isRollValid = validateRollNumber();
const isEmailValid = validateEmail();
const isDeptValid = validateDepartment();
const isEventValid = validateEventName();
const isFormValid = isNameValid && isRollValid && isEmailValid && isDeptValid && isEventValid;
if (!isFormValid) {
event.preventDefault();
const firstError = document.querySelector('.invalid');
if (firstError) {
firstError.scrollIntoView({ behavior: 'smooth', block: 'center' });
firstError.focus();
}
alert('Please fill in all required fields correctly before submitting.');
}
});
});