I am creating some regex code so I can check passwords when a customer is trying to create a account. Currently I have two pieces of code that seem to work when tested on :- http://www.regexplanet.com/simple/index.html.
The first bit is:
^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$
This makes sure:
- Password is 8 digits long
- Contains at least one one lower case letter, one upper case letter, one digit and one special character
The second part is:
\S*(.)\1{3,}\S*
This makes sure:
Any 4 or more consecutive identical characters are matched.
The question is, how can I combine them both? I have tried to no avail but they seem to be working fine seperatly.
Thanks
Why not just run two separate tests? The regex patterns above are complicated enough. Keeping them separate will make the code more understandable and will also give you the flexibility to add/remove additional tests without touching a pattern that is already working. Also, depending on which test fails, you can provide the user with a more detailed error message.