此程序在浏览器中打开一个窗口。用户输入密码及其强度级别。它从包含 72 行代码的单个 .html 文件运行,包括 HTML 结构和嵌入式 JavaScript 功能。
这个例子是为了教学如何编码。该程序是基本的,并提供简单的、基于规则的密码强度检查。它应该被视为初步工具,而不是全面的安全措施。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Password Strength Checker</title> <style> body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: flex-start; /* Align the container at the top */ height: 100vh; margin: 0; background-color: #f4f4f4; } .container { text-align: center; background-color: white; padding: 10px; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); width: 220px; /* Small width similar to audio player */ height: auto; /* Let height adjust based on content */ margin-top: 20vh; /* Moves the container down by 20% of the viewport height */ } input[type="password"] { padding: 5px; width: 180px; /* Keep it small */ margin-top: 10px; border: 1px solid #ccc; border-radius: 4px; } #strength { margin-top: 10px; font-weight: bold; } </style> </head> <body> <div class="container"> <h1>Password Checker</h1> <input type="password" id="password" placeholder="Enter password"> <div id="strength">Strength: </div> </div> <script> const passwordInput = document.getElementById('password'); const strengthDisplay = document.getElementById('strength'); passwordInput.addEventListener('input', function() { const password = passwordInput.value; const strength = calculatePasswordStrength(password); strengthDisplay.textContent = `Strength: ${strength}`; }); function calculatePasswordStrength(password) { let strength = 'Weak'; if (password.length >= 8) { strength = 'Medium'; } if (/[A-Z]/.test(password) && /[0-9]/.test(password) && /[^A-Za-z0-9]/.test(password)) { strength = 'Strong'; } return strength; } </script> </body> </html>
本·桑托拉 - 2024 年 10 月
以上是密码检查器 - JavaScript的详细内容。更多信息请关注PHP中文网其他相关文章!