JAVASCRIPT

Validate Strong Passwords with Specific Requirements

Implement a JavaScript function with a regular expression to validate strong passwords, requiring uppercase, lowercase, numbers, and special characters.

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);
}

// Examples
console.log(isStrongPassword("Password123!")); // true
console.log(isStrongPassword("password123"));   // false (no uppercase, no special char)
console.log(isStrongPassword("P@ssword"));      // false (too short)
How it works: This JavaScript snippet provides a `isStrongPassword` function that uses a complex regular expression to enforce strong password policies. It requires at least one uppercase letter, one lowercase letter, one digit, one special character, and a minimum total length of 8 characters, using lookaheads for multiple conditions.

Need help integrating this into your project?

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

Hire DigitalCodeLabs