JAVASCRIPT

Validate Strong Passwords with Regex (Min Length, Chars)

Implement strong password validation in JavaScript using regex to enforce minimum length, uppercase, lowercase, number, and special character requirements.

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

// Usage:
// console.log(isStrongPassword("P@ssword123")); // true
// console.log(isStrongPassword("password")); // false (no uppercase, no special char)
// console.log(isStrongPassword("Pass123")); // false (too short)
How it works: The `isStrongPassword` JavaScript function uses a regular expression to enforce strong password policies. It requires a minimum of 8 characters, at least one uppercase letter, one lowercase letter, one digit, and one special character. This regex leverages lookaheads (`(?=...)`) to assert the presence of these character types anywhere in the string, ensuring robust security checks for user authentication.

Need help integrating this into your project?

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

Hire DigitalCodeLabs