JAVASCRIPT

Validate Basic Password Strength Requirements

Implement a JavaScript function with regex to check basic password strength, ensuring it includes uppercase, lowercase, numbers, and minimum length.

function isStrongPassword(password) {
  // Minimum 8 characters, at least one uppercase, one lowercase, and one number.
  const hasUppercase = /[A-Z]/.test(password);
  const hasLowercase = /[a-z]/.test(password);
  const hasNumber = /[0-9]/.test(password);
  const isLongEnough = password.length >= 8;
  
  return hasUppercase && hasLowercase && hasNumber && isLongEnough;
}

// Examples
console.log(isStrongPassword("Password123")); // true
console.log(isStrongPassword("weakpass"));    // false (no uppercase, no number)
console.log(isStrongPassword("P@ssword"));   // false (no number, just special character)
console.log(isStrongPassword("pASSWORD1")); // true
console.log(isStrongPassword("Pass1"));     // false (too short)
How it works: The `isStrongPassword` JavaScript function assesses basic password strength using multiple regular expressions and a length check. It individually verifies the presence of at least one uppercase letter (`/[A-Z]/`), one lowercase letter (`/[a-z]/`), and one digit (`/[0-9]/`). Additionally, it ensures the password meets a minimum length of 8 characters. The function returns `true` only if all these conditions are met, providing a foundational layer for password policy enforcement in user registration or update forms.

Need help integrating this into your project?

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

Hire DigitalCodeLabs