Manish Sharma Manish Sharma - 3 months ago 8
ASP.NET (C#) Question

How can I achieve password regex?

I want validation using regex pattern in my project.

Following are description of regex pattern:


  1. Must be at least 8 characters in length.

  2. Must contain at least 1 UPPER CASE character.

  3. Must contain at least 1 lower case character.

  4. Must contain at least 1 number.

  5. May contain these characters:




" < > $ ~ ' ` ! @ # % ^ & * ( ) - + { } [ ] = : , . ? / | \



  1. Must not use repeating characters. (aa, 11, etc.)

  2. Must not use more than 3 sequential characters. (abcd, wxyz, 1234, etc.)


Answer

You can try this regex:

^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{8,}$

PS: It meets all conditions and I didn't include characters mentioned in section e. since you wrote May contain and dot will allow all of those.

UPDATE: As per edited question: Use this regex to meet conditions (a) - (f)

^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?:(?!.*?(.)\1).){8,}$

I would highly recomment not using regex for meeting condition (g) since it will be an awfully long regex.