好的,可以使用以下代码:
<label for="password">密码:</label> <input type="password" id="password" name="password" oninput="checkPassword()" required> <meter max="4" id="password-strength"></meter> <script> function checkPassword() { const password = document.getElementById('password').value; let strength = 0; if (password.length >= 8) { // 密码长度大于等于8 strength += 1; } if (/[A-Z]/.test(password)) { // 包含大写字母 strength += 1; } if (/[a-z]/.test(password)) { // 包含小写字母 strength += 1; } if (/\d/.test(password)) { // 包含数字 strength += 1; } document.getElementById('password-strength').value = strength; } </script>
上述代码将会输出一个密码输入框和密码强度的表盘式效果。当用户在密码框中输入内容时,上述代码将会对密码进行分析,如果密码符合四类检测项,
就会在表盘中多出四格表示密码的等级。这四项检测分别是:
- 密码长度大于等于8个字符;
- 密码中包含至少一个大写字母;
- 密码中包含至少一个小写字母;
- 密码中包含至少一个数字;
根据密码中符合的检测项,表盘上的格子数会发生变化,进而表示密码的强度。通过这个表盘式的效果,用户可以在输入密码时得到一个直观的反馈,帮助他们选择一个较强的密码。