// Password Strength Indicator document.addEventListener('DOMContentLoaded', function() { // Password strength rules const passwordRules = { minLength: 8, requireUppercase: true, requireLowercase: true, requireNumbers: true, requireSpecialChars: true, specialChars: '!@#$%^&*()_+-=[]{}|;:,.<>?' }; // Function to check password strength function checkPasswordStrength(password) { let strength = 0; const feedback = []; // Check minimum length if (password.length >= passwordRules.minLength) { strength += 20; } else { feedback.push(`At least ${passwordRules.minLength} characters`); } // Check for uppercase letters if (passwordRules.requireUppercase && /[A-Z]/.test(password)) { strength += 20; } else if (passwordRules.requireUppercase) { feedback.push('One uppercase letter'); } // Check for lowercase letters if (passwordRules.requireLowercase && /[a-z]/.test(password)) { strength += 20; } else if (passwordRules.requireLowercase) { feedback.push('One lowercase letter'); } // Check for numbers if (passwordRules.requireNumbers && /\d/.test(password)) { strength += 20; } else if (passwordRules.requireNumbers) { feedback.push('One number'); } // Check for special characters const specialCharRegex = new RegExp(`[${passwordRules.specialChars.replace(/[\[\]\\]/g, '\\$&')}]`); if (passwordRules.requireSpecialChars && specialCharRegex.test(password)) { strength += 20; } else if (passwordRules.requireSpecialChars) { feedback.push('One special character'); } // Bonus points for length if (password.length >= 12) { strength = Math.min(100, strength + 10); } if (password.length >= 16) { strength = Math.min(100, strength + 10); } return { score: strength, feedback: feedback, isValid: strength >= 100 }; } // Function to update the strength indicator UI function updateStrengthIndicator(input, result) { let container = input.parentElement.querySelector('.password-strength-container'); // Create container if it doesn't exist if (!container) { container = document.createElement('div'); container.className = 'password-strength-container'; const indicator = document.createElement('div'); indicator.className = 'password-strength-indicator'; const bar = document.createElement('div'); bar.className = 'password-strength-bar'; const text = document.createElement('div'); text.className = 'password-strength-text'; const requirements = document.createElement('ul'); requirements.className = 'password-requirements'; indicator.appendChild(bar); container.appendChild(indicator); container.appendChild(text); container.appendChild(requirements); input.parentElement.appendChild(container); } const bar = container.querySelector('.password-strength-bar'); const text = container.querySelector('.password-strength-text'); const requirements = container.querySelector('.password-requirements'); // Update bar width and color bar.style.width = result.score + '%'; // Remove all strength classes bar.className = 'password-strength-bar'; // Add appropriate class based on score if (result.score < 40) { bar.classList.add('strength-weak'); text.textContent = 'Weak'; text.className = 'password-strength-text text-weak'; } else if (result.score < 70) { bar.classList.add('strength-fair'); text.textContent = 'Fair'; text.className = 'password-strength-text text-fair'; } else if (result.score < 100) { bar.classList.add('strength-good'); text.textContent = 'Good'; text.className = 'password-strength-text text-good'; } else { bar.classList.add('strength-strong'); text.textContent = 'Strong'; text.className = 'password-strength-text text-strong'; } // Update requirements list requirements.innerHTML = ''; if (result.feedback.length > 0) { requirements.innerHTML = '