JAVASCRIPT
Validate Strong Password with Regex Criteria
Check password strength in JavaScript using regex to enforce requirements like uppercase, lowercase, numbers, special characters, and minimum length.
function isStrongPassword(password) {
const minLength = 8;
const hasUppercase = /[A-Z]/;
const hasLowercase = /[a-z]/;
const hasNumber = /[0-9]/;
const hasSpecialChar = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/;
return (
password.length >= minLength &&
hasUppercase.test(password) &&
hasLowercase.test(password) &&
hasNumber.test(password) &&
hasSpecialChar.test(password)
);
}
// Example usage:
// console.log(isStrongPassword("P@ssw0rd!")); // true
// console.log(isStrongPassword("password")); // false (no uppercase, number, special char)
How it works: This function `isStrongPassword` checks if a password meets multiple strength criteria using separate regex patterns for uppercase, lowercase, numbers, and common special characters. It also enforces a minimum length, combining these checks for comprehensive password policy validation.