-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
175 lines (139 loc) · 4.9 KB
/
Copy pathscript.js
File metadata and controls
175 lines (139 loc) · 4.9 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
const form = document.getElementById("registrationForm");
const successMessage = document.getElementById("successMessage");
const passwordStrength = document.getElementById("passwordStrength");
const passwordInput = document.getElementById("password");
const passwordToggle = document.querySelector(".password-toggle");
const fields = {
prefix: document.getElementById("prefix"),
firstName: document.getElementById("firstName"),
lastName: document.getElementById("lastName"),
username: document.getElementById("username"),
dob: document.getElementById("dob"),
password: passwordInput,
confirmPassword: document.getElementById("confirmPassword"),
countryCode: document.getElementById("countryCode"),
contact: document.getElementById("contact"),
email: document.getElementById("email"),
age: document.getElementById("age")
};
function showError(fieldName, message) {
const error = document.getElementById(`${fieldName}Error`);
const field = fields[fieldName];
if (error) {
error.textContent = message;
}
if (field) {
field.classList.toggle("invalid", Boolean(message));
}
}
function getAgeFromDob(dobValue) {
const birthDate = new Date(dobValue);
const today = new Date();
let calculatedAge = today.getFullYear() - birthDate.getFullYear();
const monthDifference = today.getMonth() - birthDate.getMonth();
if (
monthDifference < 0 ||
(monthDifference === 0 && today.getDate() < birthDate.getDate())
) {
calculatedAge -= 1;
}
return calculatedAge;
}
function validateForm() {
let isValid = true;
successMessage.textContent = "";
Object.keys(fields).forEach((fieldName) => showError(fieldName, ""));
document.getElementById("genderError").textContent = "";
if (!fields.prefix.value) {
showError("prefix", "Please select a prefix.");
isValid = false;
}
if (fields.firstName.value.trim().length < 2) {
showError("firstName", "First name must be at least 2 characters.");
isValid = false;
}
if (fields.lastName.value.trim().length < 2) {
showError("lastName", "Last name must be at least 2 characters.");
isValid = false;
}
if (fields.username.value.trim().length < 4) {
showError("username", "Username must be at least 4 characters.");
isValid = false;
}
if (!fields.dob.value) {
showError("dob", "Date of birth is required.");
isValid = false;
}
if (fields.password.value.length < 6) {
showError("password", "Password must be at least 6 characters.");
isValid = false;
}
if (fields.confirmPassword.value !== fields.password.value) {
showError("confirmPassword", "Passwords do not match.");
isValid = false;
}
const selectedGender = document.querySelector("input[name='gender']:checked");
if (!selectedGender) {
document.getElementById("genderError").textContent = "Please select gender.";
isValid = false;
}
if (!fields.countryCode.value) {
showError("countryCode", "Please select country code.");
isValid = false;
}
if (!/^[0-9\s-]{7,14}$/.test(fields.contact.value.trim())) {
showError("contact", "Enter a valid 7 to 14 digit number.");
isValid = false;
}
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(fields.email.value.trim())) {
showError("email", "Enter a valid email address.");
isValid = false;
}
const age = Number(fields.age.value);
if (!Number.isInteger(age) || age < 1 || age > 120) {
showError("age", "Age must be between 1 and 120.");
isValid = false;
}
if (fields.dob.value && age) {
const calculatedAge = getAgeFromDob(fields.dob.value);
if (calculatedAge !== age) {
showError("age", "Age should match the date of birth.");
isValid = false;
}
}
return isValid;
}
function updatePasswordStrength() {
const password = fields.password.value;
passwordStrength.className = "password-strength";
if (!password) {
passwordStrength.textContent = "";
return;
}
const hasLetter = /[A-Za-z]/.test(password);
const hasNumber = /[0-9]/.test(password);
const hasSpecial = /[^A-Za-z0-9]/.test(password);
if (hasLetter && (hasNumber || hasSpecial)) {
passwordStrength.textContent = "Strong password";
passwordStrength.classList.add("strong-password");
} else {
passwordStrength.textContent = "Weak password";
passwordStrength.classList.add("weak-password");
}
}
passwordToggle.addEventListener("click", () => {
const isHidden = passwordInput.type === "password";
passwordInput.type = isHidden ? "text" : "password";
passwordToggle.setAttribute("aria-label", isHidden ? "Hide password" : "Show password");
});
fields.password.addEventListener("input", updatePasswordStrength);
form.addEventListener("submit", (event) => {
event.preventDefault();
if (validateForm()) {
successMessage.textContent = "Form submitted successfully!";
form.reset();
updatePasswordStrength();
passwordInput.type = "password";
passwordToggle.setAttribute("aria-label", "Show password");
}
});