JAVASCRIPT

Validate Password Strength with Regex in JavaScript

Implement robust password strength validation in JavaScript using regex to enforce minimum length, uppercase, lowercase, number, and special character requirements.

function isStrongPassword(password) {
  const minLength = 8;
  const hasUppercase = /[A-Z]/;
  const hasLowercase = /[a-z]/;
  const hasNumber = /[0-9]/;
  const hasSpecialChar = /[!@#$%^&*()_+\-=\[\]{};':"\\\|.<>\/?]/;

  if (password.length < minLength) {
    return false;
  }
  if (!hasUppercase.test(password)) {
    return false;
  }
  if (!hasLowercase.test(password)) {
    return false;
  }
  if (!hasNumber.test(password)) {
    return false;
  }
  if (!hasSpecialChar.test(password)) {
    return false;
  }
  return true;
}

// Examples:
// console.log(isStrongPassword("StrongPwd1!")); // true
// console.log(isStrongPassword("weakpwd"));     // false (no uppercase, no number, no special char)
// console.log(isStrongPassword("Secure@P4ss")); // true
How it works: This snippet provides a JavaScript function to validate password strength using multiple regular expressions. It checks if the password meets several criteria: a minimum length, at least one uppercase letter, one lowercase letter, one number, and one special character. This approach allows for clear, individual checks for each strength requirement, providing robust feedback to users during registration or password changes.

Need help integrating this into your project?

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

Hire DigitalCodeLabs