JAVASCRIPT

Validate a Strong Password

Implement a robust password validation using regex, ensuring minimum length and requiring a mix of uppercase, lowercase, numbers, and special characters.

function isStrongPassword(password) {
  // Minimum 8 characters, at least one uppercase letter, one lowercase letter, one number, and one special character
  const strongPasswordRegex = new RegExp(
    "^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])(?=.{8,})"
  );
  return strongPasswordRegex.test(password);
}

// Examples
console.log(isStrongPassword("Passw0rd!")); // true
console.log(isStrongPassword("weakpass")); // false (no uppercase, no number, no special)
console.log(isStrongPassword("P@ssw0rd")); // true
console.log(isStrongPassword("password123")); // false (no uppercase, no special)
console.log(isStrongPassword("Password123")); // false (no special)
console.log(isStrongPassword("P@1")); // false (too short)
How it works: The `isStrongPassword` JavaScript function validates password strength using a single regular expression with multiple positive lookaheads. The regex ensures the password: has at least one lowercase letter `(?=.*[a-z])`, at least one uppercase letter `(?=.*[A-Z])`, at least one digit `(?=.*[0-9])`, at least one special character `(?=.*[!@#$%^&*])`, and is a minimum of 8 characters long `(?=.{8,})`.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs