JAVASCRIPT
Validate Password Strength (Min 8 chars, 1 Upper, 1 Lower, 1 Number, 1 Special)
Enforce strong password policies with a JavaScript regex requiring minimum length, uppercase, lowercase, numbers, and special characters for enhanced user security.
const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{8,}$/;
function isStrongPassword(password) {
return passwordRegex.test(password);
}
// Examples:
console.log(isStrongPassword("StrongP@ss1")); // true
console.log(isStrongPassword("weakpass")); // false (no uppercase, number, special)
console.log(isStrongPassword("P@ss1")); // false (less than 8 chars)
console.log(isStrongPassword("Password123")); // false (no special char)
How it works: This regex pattern uses positive lookaheads `(?=...)` to assert the presence of specific character types anywhere in the string, without consuming characters. It checks for at least one lowercase letter `(?=.*[a-z])`, one uppercase letter `(?=.*[A-Z])`, one digit `(?=.*\d)`, and one special character from the defined set `(?=.*[!@#$%^&*...])`. Finally, `.{8,}$` ensures the entire string is at least 8 characters long.