← Back to all snippets
JAVASCRIPT

Validating Strong Passwords

Implement a JavaScript function with regex to enforce strong password policies, requiring minimum length, uppercase, lowercase, numbers, and special characters.

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

// Example Usage:
console.log(isStrongPassword("P@ssword123")); // true
console.log(isStrongPassword("password"));    // false (no uppercase, no number, no special char)
console.log(isStrongPassword("Pass123"));     // false (no special char, too short if min 8)
How it works: This JavaScript snippet provides a `isStrongPassword` function that validates password strength using a single, complex regular expression. It enforces multiple criteria: a minimum length of 8 characters, and the inclusion of at least one uppercase letter, one lowercase letter, one digit, and one special character, ensuring 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