tags:

views:

355

answers:

5

I need the following check for strong password validation:

  • At least 7 chars
  • At least 1 uppercase char (A-Z)
  • At least 1 number (0-9)
  • At least one special char

I found and tweaked a RegEx and it's like this (sorry, I lost the reference...):

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

It's working in C# except for the fact that I need to match any special char, and I really mean ANY. In other words, I need that the "special char" be anything but numbers and lower/uppercase letters.

Edit:

For the sake of clarity, let's consider that accents are special chars, so é, ñ and the like should be considered special chars in the context of this question.

+3  A: 

I believe that :-

\w

Matches any word character.

The inverse is :-

\W

Which is what you want.

Edit

^.*(?=.{7,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W_]).*$

Test your regular expressions at :-

http://www.nregex.com/nregex/default.aspx

Paul Alan Taylor
Fails for underscore.
Yuriy Faktorovich
Nice one, Yuriy. Amended.
Paul Alan Taylor
+1  A: 
^.*(?=.{7,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).*$
Yuriy Faktorovich
`[^a-zA-Z0-9]` will match `é`: not a really special char... :)
Bart Kiers
He said "anything but numbers and lower/uppercase letters", I think é qualifies.
Yuriy Faktorovich
I think Bart is trying to say that `[^a-zA-Z0-9]` should match any character that is *not a letter*, but it will match `é` that most certainly *is* a letter (I believe that it will treat other "international" letters like å, ñ and so on as well, which it really shouldn't)
Fredrik Mörk
Tried all others solutions, and this is the only one that worked 100%. I'm really thankful!
BrunoSalvino
`é` is a lower-case "e" with acute accent. It is by no means a special char.
Bart Kiers
@Fredrik Mörk, Yuriy got what I was tring to say. I agree that `é` is a character *but* in this case, let's consider it's `special`
BrunoSalvino
@brunosalvino: sure, if those are the requirements and you are aware of it I see no problem. But I often see this kind of regex suggested where neither the OP nor the person providing the answer are aware of this, in which case they instead introduce a bug (and a rather serious one, in my eyes). That does not seem to be the case here though. (It was not me voting down, BTW)
Fredrik Mörk
+1  A: 

Take a look here: Unicode Regular Expressions and pick an Unicode class, like \p{Symbol} or \p{Punctuation}

Rubens Farias
Yes, `\p{Punctuation}` and `\p{Symbol}` is what I'd do. +1
Bart Kiers
A: 

Try this:

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

(Not C# code)

def validate (value):
    return (value.Length >= 7 &&
            value.IndexOfAny(['0', ..., '9']) >= 0 &&
            value.IndexOfAny(['A', ..., 'Z']) >= 0 &&
            value.IndexOfAny(['@', ..., ')']));

Yes I know this is not what the question required, but I believe it's much clearer, have higher performance and easier to maintain than any RegExp solution.

KennyTM
I like this too. +1.
Paul Alan Taylor
This really improves readability.
BrunoSalvino