Password validation php regex

php, regex

Solution

The following matches exactly your requirements: `^(?=.*\d.*\d)[0-9A-Za-z!@#$%*]{8,}$`

Online demo `<<< You don't need the modifiers, they are just there for testing purposes.`

Explanation

- `^` : match begin of string

- `(?=.*\d.*\d)` : positive lookahead, check if there are 2 digits

- `[0-9A-Za-z!@#$%*]{8,}` : match digits, letters and `!@#$%*` 8 or more times

- `$` : match end of string

Problem

I'm new to regex. I need to validate passwords using php with following password policy using Regex: Passwords: - Must have minimum 8 characters - Must have 2 numbers - Symbols allowed are : `! @ # $ % *` I have tried the following: `/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]$/`

Original source

Related problems