JAVASCRIPT

Validate Password Strength with Multiple Criteria Regex

Implement strong password validation using a single JavaScript regex to enforce minimum length, uppercase, lowercase, number, and special character presence.

function validatePasswordStrength(password) {
  // Must contain at least one uppercase letter, one lowercase letter, one number, one special character
  // and be at least 8 characters long.
  const passwordRegex = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>/?]).{8,}$");
  return passwordRegex.test(password);
}

// Example usage:
console.log(validatePasswordStrength("StrongP@ss1")); // true
console.log(validatePasswordStrength("weakpass"));    // false (no uppercase, no number, no special)
console.log(validatePasswordStrength("Short1!"));     // false (less than 8 characters)
How it works: This JavaScript function checks password strength using a single regular expression. It uses lookaheads (`(?=...)`) to ensure the password contains at least one lowercase letter, one uppercase letter, one digit, and one specified special character, while also enforcing a minimum length of 8 characters. This helps developers implement robust password policies.

Need help integrating this into your project?

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

Hire DigitalCodeLabs