Im trying to make a regular expresion which...
1) Consist of at least one non alphanumeric character (not including white spaces)
2) At least 7 characters long
So it would match something like...
Hello!1
P@ssword
but not:
Hello1 1
Password111
Printable View
Im trying to make a regular expresion which...
1) Consist of at least one non alphanumeric character (not including white spaces)
2) At least 7 characters long
So it would match something like...
Hello!1
P@ssword
but not:
Hello1 1
Password111
Alphanumeric characters are "[a-zA-Z0-9]", so non-alphanumeric would be "[^a-zA-Z0-9]".
You don't mention any rules of the oterh characters, so I'll assume almost any can do. This is the pattern ".".
The final expression could then be ".*[^a-zA-Z0-9].*".
Note that * is greedy (look this up!) so you may need to use ".*?" instead of ".*", but I don't think it matters in this case.
As for the length of at least 7 it gets a little trickier. Can't you just check the length in code?
Ahhh! Bloddy hell, why didn't I think of that? That makes it so much easier :)
Thanks a bunch :)Code:Regex regexPassword = new Regex(@".*[^a-zA-Z0-9].*");
if (!regexPassword.IsMatch(txtPass.Text.ToString().Trim()))
{
lblError.Text = " * Must have at least 1 non-alphanumeric character";
lblError.Visible = true;
}
else if (regexPassword.IsMatch(txtPass.Text.ToString().Trim()))
{
string temp = txtPass.Text.ToString().Trim();
if (temp.Length < 7)
{
lblError.Text = " * Password length minimum: 7.";
lblError.Visible = true;
}
}
A few notes:
1) your "else if" is unneccessary, just an "else" will do (you perform that check already in the "if")
2) No need to call ToString() on TxtPass.Text, it is already a string
You also said you wanted whitespace characters to not count.
I'd do @".*[^a-zA-Z\s\d].*" for your regex. Anything that's not a letter, whitespace, or number.