Why? Learn regex (RegExp
) and input validation.
<input type="password" id="password" placeholder="Enter password">
<p id="strength"></p>
<script>
document.getElementById("password").addEventListener("input", function() {
let pwd = this.value;
let strength = "Weak";
if(pwd.match(/[a-z]/) && pwd.match(/[A-Z]/) && pwd.match(/[0-9]/) && pwd.length >= 8) {
strength = "Strong";
} else if(pwd.match(/[a-z]/) && pwd.match(/[0-9]/) && pwd.length >= 6) {
strength = "Medium";
}
document.getElementById("strength").innerText = "Strength: " + strength;
});
</script>
Explanation:
- Uses regex to check if password has lowercase, uppercase, number, and length.
- Displays “Weak”, “Medium”, or “Strong”.
Leave a Reply