Password pattern validation regular expression

Question

Thursday, May 1, 2014 1:24 AM

Hi There,

Can someone help me build a regular expression for below password complexity?

The password must meet three of the five below conditions:
1.Uppercase character
2.Lowercase character
3.Number (0 through 9)
4.Special character: ~ ! @ # $ % ^ & * _ - + = ` | \ ( ) { } [ ] : ; “ ‘ < > , . ? / 
5.Any Unicode character categorized as an alphabetic character, but is not uppercase or lowercase (includes Asian languages Unicode characters)

Thanks, Bokes

All replies (6)

Thursday, May 1, 2014 6:32 PM ✅Answered

It might not be exactly what you're looking for, but it might come close to that. So, take a look at this regex solution for the validation of complex passwords (found on Mkyong.com):

((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})

where each part has the following meaning:

(          # Start of group
  (?=.*\d)      #   must contains one digit from 0-9
  (?=.*[a-z])       #   must contains one lowercase characters
  (?=.*[A-Z])       #   must contains one uppercase characters
  (?=.*[@#$%])      #   must contains one special symbols in the list "@#$%"
              .     #     match anything with previous condition checking
                {6,20}  #        length at least 6 characters and maximum of 20 
)           # End of group

wizend


Thursday, May 1, 2014 10:16 AM

You can't make a regular expression to know if 3 of 5 conditions are met.

You'll have to use a combination of regular expressions and code.

 

Noam B.

Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...


Thursday, May 1, 2014 2:20 PM

You should use a method to validate the password rather than a regular expression:

public static bool IsPasswordValid(string password)
        {
            if (string.IsNullOrEmpty(password))
                return false;

            byte n = 0;
            if (password.Any(c => char.IsUpper(c)))
                n++;
            if (password.Any(c => char.IsLower(c)))
                n++;
            if (password.Any(c => char.IsNumber(c)))
                n++;

            if(n > 2)
                return true;

            if (password.Contains("~") || password.Contains("@")) //...and so on
                n++;

            //...

            return n > 2;
        }

Thursday, May 1, 2014 5:30 PM

Thanks for the answers! Actually I am looking for handling this at client side, I wanted to avoid this validation at server side, therefore I was interested in Regex validation. So there is not other alternative to this?

Thanks, Bokes


Thursday, May 1, 2014 6:14 PM

Regrettably, even Regular Expressions have their limitations.    And, as Noam pointed out, getting a regex to report whether or not 3 or more of five conditions were met is dubious at best (and God only knows how one would code a Regex expression to detect condition #5).

Regex statements may be used in lieu of Magnus' string/char statements but you won't have gained much and will, perhaps, made the code unnecessarily complex.

I'm thinkin' Noam and Magnus gave great advice.   It may very well be that this particular problem is not a good fit for Regular Expressions.


Thursday, May 1, 2014 8:47 PM

Thanks for the answers again guys. I'll try one of these methods.

Thanks, Bokes