JAVASCRIPT

Enforce Strong Password Requirements with Regex

Implement strong password validation using a single regex pattern in JavaScript, ensuring passwords contain uppercase, lowercase, numbers, and special characters.

const strongPasswordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]).{8,}$/;

function isStrongPassword(password) {
  return strongPasswordRegex.test(password);
}

// Example usage:
// console.log(isStrongPassword("MyStrongP@ssw0rd")); // true
// console.log(isStrongPassword("weakpass")); // false (no uppercase, no number, no special, too short)
How it works: This regex uses positive lookaheads `(?=...)` to enforce multiple password criteria independently. It ensures the password contains at least one lowercase letter, one uppercase letter, one digit, and one special character from the defined set. It also mandates a minimum length of 8 characters for the entire string, making it ideal for robust security in user authentication.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs