JAVASCRIPT

Enforce Password Strength with Multiple Regex Criteria

Implement robust password validation in JavaScript requiring uppercase, lowercase, numbers, special characters, and minimum length using combined regular expressions.

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

    return (
        minLengthRegex.test(password) &&
        uppercaseRegex.test(password) &&
        lowercaseRegex.test(password) &&
        numberRegex.test(password) &&
        specialCharRegex.test(password)
    );
};

console.log(isStrongPassword("Passw0rd!")); // true
console.log(isStrongPassword("password"));  // false (no uppercase, number, special)
console.log(isStrongPassword("P@ssword"));  // false (no number)
console.log(isStrongPassword("P@ssw0rd"));  // false (too short)
How it works: This JavaScript function checks for password strength by combining several regular expressions. It ensures the password meets multiple criteria: a minimum length of 8 characters, at least one uppercase letter, one lowercase letter, one digit, and one common special character. Each regex independently checks for a specific requirement, and all must pass for the password to be considered strong.

Need help integrating this into your project?

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

Hire DigitalCodeLabs