JAVASCRIPT
Validate Strong Passwords with Regex for Mixed Characters
Implement a JavaScript regular expression to enforce strong password policies, requiring at least one uppercase, lowercase, number, and special character.
const isStrongPassword = (password) => {
// Minimum 8 characters, at least one uppercase letter, one lowercase letter, one number, and one special character
const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+{}\[\]:;<>,.?~\\/-]).{8,}$/;
return passwordRegex.test(password);
};
console.log(isStrongPassword("StrongPwd1!")); // true
console.log(isStrongPassword("weakpwd")); // false
console.log(isStrongPassword("OnlyDigits123")); // false (missing lowercase and special character)
How it works: This JavaScript function `isStrongPassword` validates if a password meets specific strength criteria using a regex. It requires the password to be at least 8 characters long and contain at least one uppercase letter, one lowercase letter, one digit, and one common special character. This is achieved by using positive lookaheads (`(?=...)`) for each individual condition.