Skip to Content
Course content

224: Building a Password Strength Meter

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Look, I've seen this a dozen times in code reviews: a developer builds a password strength meter and thinks they've nailed it because they're checking the length of the string. They write a simple if (password.length > 8) block and call it "Strong."

The "Length Equals Security" Fallacy

Here is why that approach fails miserably in the real world. Imagine a user enters 1234567890. That's ten characters—well over the typical eight-character threshold. By a length-only logic, your app would tell the user their password is "Strong," even though any basic brute-force script would crack that in milliseconds. Length is important, sure, but it's only one piece of the puzzle. Security is actually about entropy—the measure of unpredictability.

Measuring Entropy through Character Diversity

To build a meter that actually means something, you need to check for a variety of character sets. I usually recommend a weighted scoring system. Instead of a binary "strong or weak," we assign points for each criterion met. If the password has uppercase letters, it gets a point. Numbers? Another point. Special characters? Another point.

The most efficient way to handle this in JavaScript is using Regular Expressions (Regex). You don't need to loop through every single character manually; you just test the string against a pattern.

const checkStrength = (password) => {
  let score = 0;

  if (password.length > 8) score++;
  if (password.length > 12) score++; // Bonus for extra length
  if (/[A-Z]/.test(password)) score++;
  if (/[0-9]/.test(password)) score++;
  if (/[^A-Za-z0-9]/.test(password)) score++;

  return score;
};

Now, a 10-digit string of zeros will only score 1 point (for length), while P@ssw0rd123! will score a perfect 5. This gives the user a much more honest representation of their security.

Connecting Logic to Visual Feedback

A strength meter isn't helpful if it's just a number in the console. You need to map that score to a visual element, like a color-coded bar. I prefer using a data-attribute or a CSS class to handle the styling, keeping the JavaScript focused on the logic rather than the pixels.

You'll want to attach an event listener to the password input—specifically the input event, not change. The change event only fires when the user leaves the field, but the input event fires on every single keystroke, giving that "real-time" feel users expect.

const passwordInput = document.querySelector('#pwd');
const meterBar = document.querySelector('#meter');

passwordInput.addEventListener('input', () => {
  const score = checkStrength(passwordInput.value);
  
  // Map the score (0-5) to a CSS class
  const strengthLevels = ['very-weak', 'weak', 'fair', 'strong', 'very-strong'];
  const level = strengthLevels[score] || 'very-weak';
  
  meterBar.className = `meter-bar ${level}`;
});

One last tip: don't forget to handle the empty state. If the input is empty, the bar should probably disappear or reset to a neutral state, rather than showing "Very Weak," which can feel like the app is yelling at the user before they've even started typing.




📋 Practical Task

Build a Real-Time Password Entropy Validator

Create a password input field and a strength indicator bar. Your task is to implement a JavaScript function that calculates a strength score based on the following criteria:

  • Minimum 8 characters (1 point)
  • Contains at least one uppercase letter (1 point)
  • Contains at least one lowercase letter (1 point)
  • Contains at least one number (1 point)
  • Contains at least one special character (e.g., !, @, #, $) (1 point)
The indicator bar should change colors dynamically as the user types:
  • 0-2 points: Red (Weak)
  • 3-4 points: Yellow (Medium)
  • 5 points: Green (Strong)
Ensure the visual feedback updates instantly on every keystroke using the input event.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.