-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
274 lines (241 loc) · 9.55 KB
/
script.js
File metadata and controls
274 lines (241 loc) · 9.55 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
document.addEventListener("DOMContentLoaded", function () {
const form = document.getElementById("registrationForm");
const inputs = form.querySelectorAll("input");
// Added onChange event listener
inputs.forEach((input) => {
input.addEventListener("change", function (event) {
console.log(`Field ${this.id} changed`);
validateField(this);
});
// Added input event listener for live validation as user type each character
input.addEventListener("input", function (event) {
validateField(this);
});
});
//Added onSubmit event listener
form.addEventListener("submit", function (event) {
event.preventDefault();
console.log("Form submitted");
// Validate all fields
let isValid = true;
inputs.forEach((input) => {
if (!validateField(input)) {
isValid = false;
}
});
if (isValid) {
console.log("Form is valid");
alert("Registration successful!");
form.reset();
// Clear valid/invalid classes
inputs.forEach((input) => {
input.classList.remove("is-valid", "is-invalid");
});
// Clear password strength indicator
document.getElementById("passwordStrength").textContent = "";
} else {
console.log("Form is invalid");
}
});
});
//The main validation function for all input fields.
function validateField(field) {
const value = field.value.trim();
let isValid = true;
let errorMessage = "";
switch (field.id) {
case "fullName":
if (value.length < 5) {
isValid = false;
errorMessage = "Name must be at least 5 characters long";
} else if (!value.includes(" ")) {
isValid = false;
errorMessage = "Please enter both first and last name";
}
break;
// -----Simple & basic email validation-----
/* case "email":
if (!value.includes("@") || !value.includes(".")) {
isValid = false;
errorMessage = "Please enter a valid email address";
}
break; */
// -----Advanced email validation using regex and split method-----
case "email":
let emailError = "";
let isEmailValid = true;
// Check if there are multiple @ symbols
const atSymbols = value.split("@").length - 1;
if (atSymbols > 1) {
isEmailValid = false;
emailError = "Email cannot contain more than one @ symbol";
}
// Check if basic format is correct (has @ and .)
else if (!value.includes("@") || !value.includes(".")) {
isEmailValid = false;
emailError = "Email must contain @ and . symbols";
}
// Check characters before @
else {
const beforeAt = value.split("@")[0];
const afterAt = value.split("@")[1];
// Check length before @
if (beforeAt.length < 2) {
isEmailValid = false;
emailError = "Email must have at least 2 characters before @";
}
// Check if exactly 2 chars before @ are alphanumeric only
else if (
beforeAt.length === 2 &&
!/^[a-zA-Z0-9]{2}$/.test(beforeAt)
) {
isEmailValid = false;
emailError =
"If using only 2 characters before @, they must be alphanumeric only";
}
// Check if first character is alphanumeric
else if (!/^[a-zA-Z0-9]/.test(beforeAt)) {
isEmailValid = false;
emailError = "Email must start with a letter or number";
}
// Check for invalid special characters (allow only ., -, _)
else if (/[^a-zA-Z0-9._-]/.test(beforeAt)) {
isEmailValid = false;
emailError =
"Email can only contain letters, numbers, and the special characters . - _";
}
// Check for at least one letter before @
else if (!/[a-zA-Z]/.test(beforeAt)) {
isEmailValid = false;
emailError = "Email must contain at least one letter before @";
}
// Only proceed with domain checks if we have valid content before @
if (isEmailValid && afterAt) {
const domainParts = afterAt.split(".");
// Check domain part (between @ and .)
if (domainParts.length < 2) {
isEmailValid = false;
emailError =
"Email must have a domain with at least one alphabetical character";
} else {
const domainName = domainParts[0];
const extension = domainParts[domainParts.length - 1];
// Check for lowercase, numbers, and allowed special characters between @ and .
if (!/^[a-z0-9._-]+$/.test(domainName)) {
isEmailValid = false;
emailError =
"Domain name must contain only lowercase letters, numbers, and the special characters . - _";
}
// Check for at least one lowercase letter in domain name
else if (!/[a-z]/.test(domainName)) {
isEmailValid = false;
emailError =
"Domain name must contain at least one lowercase letter";
}
// Check for at least one character between @ and .
else if (domainName.length < 1) {
isEmailValid = false;
emailError = "Domain name must have at least one character";
}
// Check for at least 2 lowercase after final ., allowing only ., -, _
if (!/^[a-z._-]{2,}$/.test(extension)) {
isEmailValid = false;
emailError =
"Domain extension must have at least 2 lowercase letters and can include . - _";
}
}
// Check for uppercase after the first character
if (/[A-Z]/.test(value.substring(1))) {
isEmailValid = false;
emailError =
"Email cannot contain uppercase letters except as the first character";
}
// Check for invalid special characters in the entire email (allow only ., -, _)
if (/[^a-zA-Z0-9@._-]/.test(value)) {
isEmailValid = false;
emailError =
"Email can only contain letters, numbers, and the special characters . - _";
}
}
}
if (!isEmailValid) {
isValid = false;
errorMessage = emailError || "Please enter a valid email address";
}
break;
case "password":
const strength = calcPassStrength(value);
const name = document
.getElementById("fullName")
.value.toLowerCase();
if (value.length < 8) {
isValid = false;
errorMessage = "Password must be at least 8 characters long";
} else if (value.toLowerCase() === "password") {
isValid = false;
errorMessage = 'Password cannot be "password"';
} else if (
name &&
value.toLowerCase().includes(name.toLowerCase())
) {
isValid = false;
errorMessage = "Password cannot contain your name";
} else if (strength < 2) {
isValid = false;
errorMessage = "Password is too weak";
}
updatePassStrength(value);
break;
case "confirmPassword":
const password = document.getElementById("password").value;
if (value !== password) {
isValid = false;
errorMessage = "Passwords do not match";
}
break;
}
showError(field.id, errorMessage, isValid);
return isValid;
}
// Function to calculate calculate strength based on user input a lowercase, uppercase, symbol and eight characters
function calcPassStrength(password) {
let strength = 0;
if (password.length >= 8) strength++;
if (/[A-Z]/.test(password)) strength++;
if (/[0-9]/.test(password)) strength++;
if (/[^A-Za-z0-9]/.test(password)) strength++;
return strength;
}
function showError(fieldId, message, isValid) {
const errorDiv = document.getElementById(`${fieldId}Error`);
const inputField = document.getElementById(fieldId);
if (!isValid) {
errorDiv.textContent = message;
errorDiv.style.display = "block";
inputField.classList.add("is-invalid");
inputField.classList.remove("is-valid");
} else {
errorDiv.style.display = "none";
inputField.classList.remove("is-invalid");
inputField.classList.add("is-valid");
}
}
// Function to update, display password strength and assign class for css styling
function updatePassStrength(password) {
const strengthDiv = document.getElementById("passwordStrength");
const strength = calcPassStrength(password);
let strengthText = "";
let strengthClass = "";
if (strength < 2) {
strengthText = "Weak Password";
strengthClass = "strength-weak";
} else if (strength < 4) {
strengthText = "Medium Password";
strengthClass = "strength-medium";
} else {
strengthText = "Strong Password";
strengthClass = "strength-strong";
}
strengthDiv.textContent = strengthText;
strengthDiv.className = "password-strength " + strengthClass;
}