@@ -6,6 +6,12 @@ import {
66 hideError ,
77 clearErrors ,
88} from "/js/shared.js" ;
9+ import {
10+ calculateStrength ,
11+ updateStrengthBar ,
12+ initPasswordStrengthMeter ,
13+ initPasswordRequirements ,
14+ } from "/js/utils/password-validation.js" ;
915
1016document . addEventListener ( "DOMContentLoaded" , ( ) => {
1117 const form = document . querySelector ( "#registerForm" ) ;
@@ -31,33 +37,14 @@ document.addEventListener("DOMContentLoaded", () => {
3137 } ) ;
3238 } ) ;
3339
34- // Toggle password requirements visibility
40+ // Initialize password requirements visibility toggle
3541 const passwordRules = document . getElementById ( "password-requirements" ) ;
3642 const passwordError = document . querySelector ( "#passwordError" ) ;
37- if ( passwordField && passwordRules ) {
38- passwordField . addEventListener ( "focus" , ( ) => {
39- passwordRules . classList . remove ( "d-none" ) ;
40- // Hide the error while user is editing
41- passwordError . classList . add ( "d-none" ) ;
42- } ) ;
43-
44- passwordField . addEventListener ( "blur" , ( ) => {
45- passwordRules . classList . add ( "d-none" ) ;
46- } ) ;
47- }
43+ initPasswordRequirements ( passwordField , passwordRules , passwordError ) ;
4844
49- // Password strength UI
50- passwordField . addEventListener ( "input" , ( ) => {
51- const passwordStrength = document . getElementById ( "password-strength" ) ;
52- const password = passwordField . value ;
53- if ( password ) {
54- passwordStrength . classList . remove ( "d-none" ) ;
55- const score = calculateStrength ( password ) ;
56- updateStrengthBar ( score , strengthLevel , strengthLabel ) ;
57- } else {
58- passwordStrength . classList . add ( "d-none" ) ;
59- }
60- } ) ;
45+ // Initialize password strength meter
46+ const passwordStrength = document . getElementById ( "password-strength" ) ;
47+ initPasswordStrengthMeter ( passwordField , passwordStrength , strengthLevel , strengthLabel ) ;
6148} ) ;
6249
6350async function handleRegistration ( event ) {
@@ -160,29 +147,3 @@ async function handleRegistration(event) {
160147 return msg . toLowerCase ( ) . includes ( "password" ) ;
161148 }
162149}
163-
164- function calculateStrength ( password ) {
165- let score = 0 ;
166- if ( password . length >= 8 ) score ++ ; // Rule 1: Length
167- if ( / [ A - Z ] / . test ( password ) ) score ++ ; // Rule 2: Uppercase
168- if ( / [ a - z ] / . test ( password ) ) score ++ ; // Rule 3: Lowercase
169- if ( / [ 0 - 9 ] / . test ( password ) ) score ++ ; // Rule 4: Number
170- if ( / [ ^ A - Z a - z 0 - 9 ] / . test ( password ) ) score ++ ; // Rule 5: Special character
171- return score ;
172- }
173-
174- function updateStrengthBar ( score , strengthLevel , strengthLabel ) {
175- const levels = [
176- { width : "0%" , color : "" , label : "" } ,
177- { width : "20%" , color : "bg-danger" , label : "Very Weak" } ,
178- { width : "40%" , color : "bg-warning" , label : "Weak" } ,
179- { width : "60%" , color : "bg-info" , label : "Fair" } ,
180- { width : "80%" , color : "bg-primary" , label : "Strong" } ,
181- { width : "100%" , color : "bg-success" , label : "Very Strong" } ,
182- ] ;
183-
184- const level = levels [ score ] || levels [ 0 ] ;
185- strengthLevel . style . width = level . width ;
186- strengthLevel . className = `progress-bar ${ level . color } ` ;
187- strengthLabel . textContent = `Password strength: ${ level . label } ` ;
188- }
0 commit comments