I would like to use a regular expression in the ASP.NET membership. What is a regular express for the below?
- at least 8 characters long
- include at least one upper case letter
- one lower case letter
- one number
I would like to use a regular expression in the ASP.NET membership. What is a regular express for the below?
try this..
^((?=.*\d)(?=.*[A-Z])(?=.*[a-z]).{8,})
You could use something like that:
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d=:;<>,~!@#\\$%/^&)(\[\]+-]{8,}$
Test it here.
You may also want to learn about the "?=" thing, which is called "positive lookahead" here.
In short, when all three lookaheads (.*\d
and .*[a-z]
and .*[A-Z]
) are matched (and are discarded), the main regex [a-zA-Z\d=:;<>,~!@#\\$%/^&)(\[\]+-]{8,}
can be matched too.
Do you have to do this in one regex? I would make each of those rules one regex, and test for them individually. I suspect you code will end up being simpler, and you'll save yourself and whoever has to maintain your application several headaches.