Click here to Skip to main content
15,671,318 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Dear Friends,

am working on c# project

Can any one help me to write a regular expression to validate the password
entered on the text field in ASP.NET.
The rules for the password are:

1) There must be Upper case letters in the password.
2) There must be lower case letters in the password.
3)There must be numbers in the password.
4) Password must be minimum 8 characters
5) Password cannot contain illegal characters, such as =, <, >, ;, :, '
, " or comma (equal sign, greater sign, less than sign, semicolon, colon,
single quote, double quotes, comma).

Please help me,

Thanks in @dvance.

This is one of the typical examples where using Regex would be extremely inconvenient, because you would have to foresee all possible permutation, which is nearly impossible: the order of characters is not constrained and maximum length is not limited.

The solution is much simpler than that: scan all characters in the loop and classify them all into 4 (#4 is not used, check up the length in the very beginning) categories you described. You would need to remember only the number of occurences. Then, for each category, apply your criteria on multiplicity: in #1, #2 and #3, must be greater then zero, in #5, should be zero. For #5, instead of "such as", list all and write it in one string, to calculate that a character is not found in that string.

These methods will help you:[^] (for rule #5, check "not contains"),[^]:[^],[^],[^].

Share this answer
Thanks7872 3-Oct-13 3:16am    
Looks more promising than mine. I think mine would also be helpful for the OP.
Sergey Alexandrovich Kryukov 3-Oct-13 9:15am    
Thank you, Rohan.
Hi, I have a post on CP on the same topic. Please have a look at this: Strong Password Validation[^]
Share this answer
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
        <asp:RegularExpressionValidator ID="revPassword" runat="server" ErrorMessage="Invalid Password Charecters"
        ValidationExpression="^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8}$" ControlToValidate="txtPassword" ForeColor="Red" ></asp:RegularExpressionValidator>

Matches Cases



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