Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression validation issues

Status
Not open for further replies.

LittlBUGer

Programmer
Apr 26, 2006
81
US
Hello. I've been trying to get passwords on new user registrations restricted to have at least 1 uppercase letter, 1 lowercase letter, 1 number, no spaces, and be a minimum of 8 characters long. I'm pretty positive I've got the right regular expression (I got it off of regexlib.com) but for some reason it just does not want to work properly. Here's the expression:

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

And here's the code for the control (textbox):

Code:
		<asp:TextBox ID="password" runat="server"></asp:TextBox>
		<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="password" ErrorMessage="*" />
		<asp:RegularExpressionValidator ID="RegularExpressionValidator3" ValidationExpression="^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$" ControlToValidate="password" runat="server" ErrorMessage="!" />

Now, on the page, when I try to enter something like, Password12, it doesn't work. It DOES work if I change the {8,} to {2,} which makes no sense to me. Then when I try to use passord12W, it does NOT work, like it can't handle the uppercase letter at the end.

All I want is to be able to handle a password with at least 1 uppercase, 1 lowercase, and 1 number and no spaces, in ANY order, being at least 8 characters long. Can someone please help me here? I've tried so many different combinations now I'm starting to lose track which is what. Thanks. :)

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
I've tried your code with VS2008 (on XP, .NET v3.5) and it seems to be working well. So, I have no idea what's wrong at your place.

On the other hand, I don't see the 'no spaces' criteria in your regexp. You should use \w{8,} (or equivalent) instead of .{8,}.
 
Sorry about that, it actually should be the following to handle the no spaces as well:

Code:
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{8,}$

I'm using VS2005 and .NET 2.0. Just very random behavior it seems. Will changing the '.' to '\w' allow for any characters, special or not, as long as the other requirements are met? Thanks.

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
I just tested it with the \w instead, now it wont accept special characters. When I use the {8,}, it's requiring at least 14 characters or it wont work. By changing it to {2,} it only needs 8, but it still wont accept something like:

hello12D

Because there's no uppercase in the front, only in the back, even though order shouldn't matter. This is driving me crazy and makes no sense. I've done regular expressions before, in the same code even, and have never had such grief. :(

"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
 
The \w is only for alphanumeric (A-Z0-9, no special chars allowed).
But your problem is something else. Your regexp is good (I've tested it with VS2008, and RegexCoach as well).
Perhaps it is a bug in .NET2? I don't know, but I have no other guess...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top