JAVASCRIPT
Validate Password Strength with Combined Regex
Create a robust regex in JavaScript to enforce strong password policies, requiring a mix of specific character types and a minimum length.
function isStrongPassword(password) {
// At least 8 characters long
// Contains at least one uppercase letter
// Contains at least one lowercase letter
// Contains at least one digit
// Contains at least one special character (!@#$%^&*()_+~`|}{[]:;?><,./-=)
const strongPasswordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+~`|}{[\]:;?><,./\-=])[A-Za-z\d!@#$%^&*()_+~`|}{[\]:;?><,./\-]{8,}$/;
return strongPasswordRegex.test(password);
}
// Examples:
console.log(isStrongPassword("P@ssw0rd!")); // true
console.log(isStrongPassword("MyStrongP@ss1")); // true
console.log(isStrongPassword("weakpass")); // false (no uppercase, no digit, no special)
console.log(isStrongPassword("P@ss1")); // false (too short)
How it works: This regex utilizes lookaheads `(?=...)` to assert multiple conditions without consuming characters. `(?=.*[a-z])` ensures at least one lowercase letter, `(?=.*[A-Z])` for uppercase, `(?=.*\d)` for a digit, and `(?=.*[!@#$%^&*()_+~`|}{[\]:;?><,./\-=])` for a special character. Finally, `[A-Za-z\d!@#$%^&*()_+~`|}{[\]:;?><,./\-]{8,}$` matches at least 8 of these allowed characters, anchoring the start and end of the string, enforcing minimum length and character set.