@@ -8,6 +8,8 @@ const passwordInput = document.getElementById("password");
88const generateBtn = document . getElementById ( "generateBtn" ) ;
99const copyBtn = document . getElementById ( "copyBtn" ) ;
1010const message = document . getElementById ( "message" ) ;
11+ const strengthText = document . getElementById ( "strengthText" ) ;
12+ const strengthBar = document . getElementById ( "strengthBar" ) ;
1113
1214const 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+
3061function 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
5183lengthInput . 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
5592generateBtn . addEventListener ( "click" , generatePassword ) ;
@@ -69,3 +106,4 @@ copyBtn.addEventListener("click", async () => {
69106} ) ;
70107
71108generatePassword ( ) ;
109+ updateStrengthUI ( ) ;
0 commit comments