JAVASCRIPT
Validate Password Strength with Regex
Implement a JavaScript function to validate password strength using regex, ensuring minimum length and character type requirements for security.
function isStrongPassword(password) {
const minLength = 8;
const hasUppercase = /[A-Z]/.test(password);
const hasLowercase = /[a-z]/.test(password);
const hasNumber = /[0-9]/.test(password);
const hasSpecialChar = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password);
return (
password.length >= minLength &&
hasUppercase &&
hasLowercase &&
hasNumber &&
hasSpecialChar
);
}
// Usage:
console.log(isStrongPassword("P@ssw0rd1!")); // true
console.log(isStrongPassword("weakpass")); // false (no uppercase, no number, no special)
How it works: This JavaScript function `isStrongPassword` checks a password against several essential security criteria using individual regular expressions. It verifies a minimum length of 8 characters and the presence of at least one uppercase letter, one lowercase letter, one number, and one special character. This helps web applications enforce robust password policies.