· 9 months ago · Mar 06, 2025, 04:40 PM
1function validatePassword(password) {
2 let isValid = true;
3 let errors = [];
4
5 if (password.length < 6 || password.length > 10) {
6 isValid = false;
7 errors.push("Password must be between 6 and 10 characters");
8 }
9
10 for (let char of password) {
11 let charCode = char.charCodeAt(0);
12
13 if (!(charCode >= 48 && charCode <= 57) && !(charCode >= 65 && charCode <= 90) && !(charCode >= 97 && charCode <= 122)) {
14 isValid = false;
15 errors.push("Password must consist only of letters and digits");
16 break;
17 }
18 }
19
20 let digitCount = 0;
21 for (let char of password) {
22 let charCode = char.charCodeAt(0);
23 if (charCode >= 48 && charCode <= 57) {
24 digitCount++;
25 }
26 }
27
28 if (digitCount < 2) {
29 isValid = false;
30 errors.push("Password must have at least 2 digits");
31 }
32
33
34 if (isValid) {
35 console.log("Password is valid");
36 } else {
37 console.log(errors.join('\n'))
38 }
39}