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

am working on asp.net 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.
Posted

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:
http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx[^] (for rule #5, check "not contains"),
http://msdn.microsoft.com/en-us/library/system.char.aspx[^]:
http://msdn.microsoft.com/en-us/library/d1x97616.aspx[^],
http://msdn.microsoft.com/en-us/library/yk2b3t2y.aspx[^],
http://msdn.microsoft.com/en-us/library/9s91f3by.aspx[^].

—SA
   
v2
Comments
Thanks7872 3-Oct-13 3:16am
   
Looks more promising than mine. I think mine would also be helpful for the OP.
   
Thank you, Rohan.
—SA
Hi, I have a post on CP on the same topic. Please have a look at this: Strong Password Validation[^]
   
XML
<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

11aaaAAA

111111aA

1aaaaaaA
   

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