JAVASCRIPT

Validate Password Strength with Regex Criteria

Implement a strong password validation using a regular expression in JavaScript, requiring uppercase, lowercase, digits, special characters, and minimum length.

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

console.log(isStrongPassword("P@ssw0rd!")); // true
console.log(isStrongPassword("password")); // false (no uppercase, no special, no digit)
How it works: This JavaScript function uses a complex regular expression with positive lookaheads to enforce strong password policies. It ensures the password contains at least one lowercase letter, one uppercase letter, one digit, one common special character, and is a minimum of 8 characters long.

Need help integrating this into your project?

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

Hire DigitalCodeLabs