JAVASCRIPT

Validate Strong Password with Regex

Enforce strong password policies using a comprehensive JavaScript regex pattern. Ensure passwords meet criteria for length, uppercase, lowercase, numbers, and special characters.

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

// Example Usage:
console.log(isStrongPassword("Password123!"));  // true
console.log(isStrongPassword("P@ssw0rd"));      // true
console.log(isStrongPassword("weakpass"));      // false (no uppercase, no digit, too short)
console.log(isStrongPassword("Password123"));   // false (no special char)
console.log(isStrongPassword("password123!"));  // false (no uppercase)
How it works: This JavaScript function `isStrongPassword` validates a password against several common security criteria using a single powerful regular expression. It employs positive lookaheads `(?=...)` to ensure the password is at least 8 characters long and includes a mix of uppercase letters, lowercase letters, numbers, and special characters, promoting better user security practices.

Need help integrating this into your project?

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

Hire DigitalCodeLabs