JAVASCRIPT

Enforce Strong Passwords with Regex Validation

Implement a JavaScript regex to validate password strength, requiring a mix of uppercase, lowercase, numbers, and special characters, along with a minimum length.

const strongPasswordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{8,}$/;

function isStrongPassword(password) {
  return strongPasswordRegex.test(password);
}

console.log(isStrongPassword('P@ssword123'));    // true
console.log(isStrongPassword('password123'));    // false (no uppercase)
console.log(isStrongPassword('PASSWORD123'));    // false (no lowercase)
console.log(isStrongPassword('P@ssword'));      // false (no number)
console.log(isStrongPassword('Password123'));   // false (no special char)
console.log(isStrongPassword('P@s1'));          // false (too short)
How it works: This powerful regex uses positive lookaheads `(?=...)` to enforce multiple password criteria simultaneously without consuming characters. It requires at least one lowercase letter `(?=.*[a-z])`, one uppercase letter `(?=.*[A-Z])`, one digit `(?=.*\d)`, and one special character `(?=.*[!@#$%...])`. Finally, `.{8,}` ensures a minimum length of 8 characters. Each lookahead asserts a condition without advancing the regex engine's position, allowing all conditions to be checked against the same string.

Need help integrating this into your project?

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

Hire DigitalCodeLabs