← Back to all snippets
JAVASCRIPT

Validate Strong Password Requirements

Implement a JavaScript regex pattern to enforce strong password policies, requiring 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 strongRegex = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])(?=.{8,})");
  return strongRegex.test(password);
}

// Example Usage:
console.log(isStrongPassword("P@ssw0rd!")); // true
console.log(isStrongPassword("password")); // false (missing uppercase, number, special)
console.log(isStrongPassword("P@ss1")); // false (too short)
How it works: This JavaScript function checks if a password meets specific strength criteria using a regular expression. The regex ensures the password contains at least 8 characters, including at least one uppercase letter, one lowercase letter, one number, and one special character (from a predefined set). This helps improve user account security by encouraging complex passwords.

Need help integrating this into your project?

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

Hire DigitalCodeLabs