← Back to all snippets
JAVASCRIPT

Enforce Strong Passwords with Regex

Validate password strength in JavaScript using regex to ensure it contains a mix of uppercase, lowercase, numbers, and special characters, meeting minimum length requirements.

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

// Usage:
console.log(isStrongPassword("P@ssword123")); // true
console.log(isStrongPassword("password"));     // false (no uppercase, no number, no special char)
console.log(isStrongPassword("Short1!"));      // false (less than 8 chars)
How it works: This function `isStrongPassword` checks a password string against a regular expression for common strength requirements. It uses lookaheads (`(?=...)`) to assert the presence of at least one lowercase letter, one uppercase letter, one digit, and one specified special character, all while ensuring the total length is at least 8 characters.

Need help integrating this into your project?

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

Hire DigitalCodeLabs