Skip to content

Commit 47f14a5

Browse files
committed
Add password strength indicator with dynamic UI updates
1 parent 9fbe78d commit 47f14a5

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

Day #14 - Password Generator/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ <h1>Password Generator</h1>
2525
<button id="copyBtn" type="button">Copy</button>
2626
</div>
2727

28+
<div class="strength-wrap">
29+
<div class="strength-head">
30+
<span>Strength</span>
31+
<span id="strengthText">Medium</span>
32+
</div>
33+
<div class="strength-track" aria-hidden="true">
34+
<div id="strengthBar" class="strength-bar"></div>
35+
</div>
36+
</div>
37+
2838
<button id="generateBtn" type="button" class="generate">Generate Password</button>
2939
<p id="message" aria-live="polite"></p>
3040
</main>

Day #14 - Password Generator/script.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const passwordInput = document.getElementById("password");
88
const generateBtn = document.getElementById("generateBtn");
99
const copyBtn = document.getElementById("copyBtn");
1010
const message = document.getElementById("message");
11+
const strengthText = document.getElementById("strengthText");
12+
const strengthBar = document.getElementById("strengthBar");
1113

1214
const chars = {
1315
uppercase: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
@@ -27,6 +29,35 @@ function getSelectedPool() {
2729
return pool;
2830
}
2931

32+
function getStrengthLevel() {
33+
let score = 0;
34+
const length = Number(lengthInput.value);
35+
36+
if (length >= 10) score += 1;
37+
if (length >= 14) score += 1;
38+
if (uppercaseInput.checked) score += 1;
39+
if (lowercaseInput.checked) score += 1;
40+
if (numbersInput.checked) score += 1;
41+
if (symbolsInput.checked) score += 1;
42+
43+
if (score <= 2) {
44+
return { label: "Weak", width: "33%", color: "#e24a4a" };
45+
}
46+
47+
if (score <= 4) {
48+
return { label: "Medium", width: "66%", color: "#f0a500" };
49+
}
50+
51+
return { label: "Strong", width: "100%", color: "#2ea043" };
52+
}
53+
54+
function updateStrengthUI() {
55+
const level = getStrengthLevel();
56+
strengthText.textContent = level.label;
57+
strengthBar.style.width = level.width;
58+
strengthBar.style.backgroundColor = level.color;
59+
}
60+
3061
function generatePassword() {
3162
const length = Number(lengthInput.value);
3263
const pool = getSelectedPool();
@@ -46,10 +77,16 @@ function generatePassword() {
4677

4778
passwordInput.value = result;
4879
message.textContent = "Password generated.";
80+
updateStrengthUI();
4981
}
5082

5183
lengthInput.addEventListener("input", () => {
5284
lengthValue.textContent = lengthInput.value;
85+
updateStrengthUI();
86+
});
87+
88+
[uppercaseInput, lowercaseInput, numbersInput, symbolsInput].forEach((input) => {
89+
input.addEventListener("change", updateStrengthUI);
5390
});
5491

5592
generateBtn.addEventListener("click", generatePassword);
@@ -69,3 +106,4 @@ copyBtn.addEventListener("click", async () => {
69106
});
70107

71108
generatePassword();
109+
updateStrengthUI();

Day #14 - Password Generator/style.css

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,34 @@ h1 {
5757
margin-bottom: 14px;
5858
}
5959

60+
.strength-wrap {
61+
margin-bottom: 14px;
62+
}
63+
64+
.strength-head {
65+
display: flex;
66+
justify-content: space-between;
67+
font-size: 14px;
68+
font-weight: 700;
69+
margin-bottom: 6px;
70+
}
71+
72+
.strength-track {
73+
width: 100%;
74+
height: 8px;
75+
background: #ececec;
76+
border-radius: 999px;
77+
overflow: hidden;
78+
}
79+
80+
.strength-bar {
81+
height: 100%;
82+
width: 50%;
83+
border-radius: inherit;
84+
transition: width 0.2s ease, background-color 0.2s ease;
85+
background: #f0a500;
86+
}
87+
6088
#password {
6189
width: 100%;
6290
padding: 10px;

0 commit comments

Comments
 (0)