Summary: In this programming example, you will learn to write a JavaScript program to check the strength of the given password.

There are a few different ways you can check the strength of a password in JavaScript. Here’s one example of how you might approach this:

function checkPasswordStrength(password) {
  // Initialize variables
  let strength = 0;
  let upperCase = false;
  let lowerCase = false;
  let numeric = false;
  let special = false;

  // Check for minimum length
  if (password.length >= 8) {
    strength += 1;
  }

  // Check for uppercase characters
  if (/[A-Z]/.test(password)) {
    upperCase = true;
    strength += 1;
  }

  // Check for lowercase characters
  if (/[a-z]/.test(password)) {
    lowerCase = true;
    strength += 1;
  }

  // Check for numeric characters
  if (/[0-9]/.test(password)) {
    numeric = true;
    strength += 1;
  }

  // Check for special characters
  if (/[!@#\$%\^&\*]/.test(password)) {
    special = true;
    strength += 1;
  }

  // Check for all 4 character types
  if (upperCase && lowerCase && numeric && special) {
    strength += 1;
  }

  // Return the strength
  return strength;
}

// Example usage
let password = "P@ssw0rd!";
let strength = checkPasswordStrength(password);
console.log(strength); // Outputs 6

This function uses regular expressions to check if the password contains uppercase characters, lowercase characters, numeric characters, and special characters.

It also checks for a minimum length of 8 characters and gives an additional point if the password contains all four character types.

The strength is then returned as a number from 0 to 6, with higher numbers indicating a stronger password.

Of course, this is just one way to check password strength, and you might want to customize the function to fit your specific requirements.

If you want a more comprehensive password strength estimator then you should consider using a library like zxcvbn.

Adarsh Kumar

I am an engineer by education and writer by passion. I started this blog to share my little programming wisdom with other programmers out there. Hope it helps you.

Leave a Reply