-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
141 lines (113 loc) · 4.56 KB
/
script.js
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
const generatedPassword = document.getElementById("generated-password");
const copyIcon = document.getElementById("copy-icon");
const charRange = document.getElementById("char-range");
const charNumber = document.getElementById("char-number");
const uppercaseCheckbox = document.getElementById("uppercase");
const lowercaseCheckbox = document.getElementById("lowercase");
const numbersCheckbox = document.getElementById("numbers");
const symbolsCheckbox = document.getElementById("symbols");
const generateBtn = document.getElementById("generate-btn");
const uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
const numbers = "0123456789";
const symbols = "!@#$%&*?";
// SHOWING THE VALUE OF THE SLIDER RANGE
charRange.addEventListener("input", function () {
charNumber.textContent = charRange.value;
});
// GENERATING THE PASSWORD
generateBtn.addEventListener("click", generatePassword);
// if the user plays around with the buttons and range then the code will still function as expected with these eventlisteners added to the js code:
uppercaseCheckbox.addEventListener("change", generatePassword);
lowercaseCheckbox.addEventListener("change", generatePassword);
numbersCheckbox.addEventListener("change", generatePassword);
symbolsCheckbox.addEventListener("change", generatePassword);
charRange.addEventListener("input", generatePassword);
function generatePassword() {
let password = "";
let length = parseInt(charRange.value);
let chars = "";
if (uppercaseCheckbox.checked) chars += uppercaseLetters;
if (lowercaseCheckbox.checked) chars += lowercaseLetters;
if (numbersCheckbox.checked) chars += numbers;
if (symbolsCheckbox.checked) chars += symbols;
if (!chars) {
generatedPassword.textContent = "Select at least one option";
generatedPassword.classList.add("error");
setTimeout(() => {
generatedPassword.textContent = "P4$5W0rD!";
generatedPassword.classList.remove("error");
}, 1500);
return;
}
for (let i = 0; i < length; i++) {
password += chars[Math.floor(Math.random() * chars.length)];
}
generatedPassword.classList.remove("error");
generatedPassword.textContent = password;
calculatePassWordStrength(password);
}
// CALCULATE PASSWORD STRENGTH
function calculatePassWordStrength(password) {
const strengthImgMeter = document.getElementById("strength-img-meter");
const length = password.length;
const hasUpperCase = /[A-Z]/.test(password);
const hasLowerCase = /[a-z]/.test(password);
const hasNumbers = /[0-9]/.test(password);
const hasSymbols = /[!@#$%&*?]/.test(password);
let typeCount = 0;
if (hasUpperCase) typeCount++;
if (hasLowerCase) typeCount++;
if (hasNumbers) typeCount++;
if (hasSymbols) typeCount++;
let strengthLevel = "";
if (length === 0) {
strengthLevel = "empty";
} else if (length <= 4) {
strengthLevel = "too-weak";
} else if (length <= 8) {
strengthLevel = typeCount >= 2 ? "weak" : "too-weak";
} else if (length <= 13) {
strengthLevel = typeCount >= 2 ? "medium" : "weak";
} else if (length > 13) {
strengthLevel = typeCount >= 3 ? "strong" : "medium";
}
if (strengthLevel === "empty") {
strengthImgMeter.src = "assets/images/empty.png";
strengthImgMeter.alt = "No password entered";
} else if (strengthLevel === "too-weak") {
strengthImgMeter.src = "assets/images/too-weak.png";
strengthImgMeter.alt = "Password strength is too weak";
} else if (strengthLevel === "weak") {
strengthImgMeter.src = "assets/images/weak.png";
strengthImgMeter.alt = "Password strength is weak";
} else if (strengthLevel === "medium") {
strengthImgMeter.src = "assets/images/medium.png";
strengthImgMeter.alt = "Password strength is medium";
} else if (strengthLevel === "strong") {
strengthImgMeter.src = "assets/images/strong.png";
strengthImgMeter.alt = "Password strength is strong";
}
}
// COPY PASSWORD
copyIcon.addEventListener("click", copyPassword);
function copyPassword() {
const passwordText = generatedPassword.textContent;
if (passwordText === "P4$5W0rD!" || passwordText === "Tick at least 1 box") {
return;
}
navigator.clipboard
.writeText(passwordText)
.then(() => {
const originalText = generatedPassword.textContent;
generatedPassword.textContent = "Copied!";
generatedPassword.classList.add("copied");
setTimeout(() => {
generatedPassword.textContent = originalText;
generatedPassword.classList.remove("copied");
}, 2000);
})
.catch((err) => {
console.log("Failed to copy: ", err);
});
}