Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i need:
- 12-15 characters
- contain at least 1 capital letter OR 1 special character
- contain at least 1 small letter
- contain at least 1 number

this is the regex i get so far, but still not complete yet.
C#
^(?=^.{12,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$
Posted

^(?=^.{12,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z!@#$%^&*()])(?=.*[a-z]).*$
 
Share this answer
 
This will do the trick:
^(?=.*[A-Z!@#$%^&*()])(?=.*[a-z])(?=.*\d).*{12,15}$

It's a series of positive lookaheads, which all verify a match without consuming any characters. Then the length validation consumes the entire string, if it's the correct length.

Match capital or special (this depends on your definition of "special"):
(?=.*[A-Z!@#$%^&*()])

Match lowercase letter:
(?=.*[a-z])

Match number:
(?=.*\d)

Verify length (this is where you would also restrict any invalid characters):
.*{12,15}
 
Share this answer
 
Use this Regular expression :

XML
<asp:RegularExpressionValidator ID="rev" runat="server" ControlToValidate="txt"
           ErrorMessage="Message"
           ValidationExpression="(?=^.{8,}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s)[0-9a-zA-Z!@#$%^&*()]*$"
            ValidationGroup="validation">*</asp:RegularExpressionValidator>
 
Share this answer
 
Don't use a regex - it's not a "text matching" task.
Instead use code - by all means use a bunch of different regexes (or even a single regex to group the characters into their types) but check number and so forth in code - it's easier to wrote, and a lot easier to understand and maintain.

A regex to do that would be horribly difficult to understand - unlike the equivalent code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900