Reputation: 676
I need the following check for strong password validation:
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.
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.
Upvotes: 7
Views: 11427
Reputation: 3
Regex Rx = null;
Rx = new System.Text.RegularExpressions.Regex("^(?=.*?[A-Za-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{7,}$");
if (Rx.IsMatch(textBox3.Text))
{
textBox3.BackColor = Color.Green;
textBox3.ForeColor = Color.White;
MessageBox.Show("Password is (correct) format ");
}
else
{
textBox3.BackColor = Color.DarkRed;
textBox3.ForeColor = Color.White;
MessageBox.Show("Contact is (in-correct) format");
}
Upvotes: 0
Reputation: 10680
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
Upvotes: 4
Reputation: 523784
(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.
Upvotes: 5
Reputation: 655825
Try this:
^(?=.{7,})(?=.*?\d)(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[@'#.$;%^&+=!"()*,-/:<>?])
Upvotes: 0
Reputation: 57996
Take a look here: Unicode Regular Expressions and pick an Unicode class, like \p{Symbol}
or \p{Punctuation}
Upvotes: 1
Reputation: 68747
^.*(?=.{7,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).*$
Upvotes: 10