JAVASCRIPT

Enforce Strong Password Requirements

Implement a JavaScript regex pattern to validate strong passwords, requiring a minimum length, uppercase, lowercase, numbers, and special characters.

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

console.log(isStrongPassword("MyPass123!")); // true
console.log(isStrongPassword("weakpass")); // false (no uppercase, no special, too short)
console.log(isStrongPassword("Password123")); // false (no special character)
How it works: This function `isStrongPassword` validates a password against strength criteria. It uses positive lookaheads `(?=...)` to assert conditions without consuming characters: `(?=.*[a-z])` for a lowercase letter, `(?=.*[A-Z])` for an uppercase letter, `(?=.*\d)` for a digit, and `(?=.*[!@#$%^&*...])` for a special character from a defined set. Finally, `.{8,}$` ensures the password is at least 8 characters long, making it a robust strength check.

Need help integrating this into your project?

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

Hire DigitalCodeLabs