Regular Expressions for strong password in asp.net

For security consideration its always good to apply a strong password for authentication, these are few regular expressions I found to enable strong passwords in ASP.net text field control

^.*(?=.{10,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$

Must be at least 10 characters , contain at least one one lower case letter, one upper case letter, one digit and one special character, Valid special characters (which are configurable) are -   @#$%^&+=

(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$

Passwords will contain at least (1) upper case letter
Passwords will contain at least (1) lower case letter
Passwords will contain at least (1) number or special character
Passwords will contain at least (8) characters in length
Password maximum length should not be arbitrarily limited

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

Must be at least 8 characters , contain at least one lower case letter, one upper case letter and one digit

14 Responses

  1. Thanks. Very useful. This got me most of the way there. Our rules are a little different: password must be at least 6 characters and must contain at least 3 of the following 4 categories:
    uppercase, lowercase, digits, symbols (e.g. !, $, #, %).

    Here’s my pattern:
    (?=^.{6,}$)((?=.*\d)(?=.*\W+)(?=.*[A-Z]))|((?=.*\d)(?=.*\W+)(?=.*[a-z]))|((?=.*\W+)(?=.*[A-Z])(?=.*[a-z]))|((?=.*\d)(?=.*[A-Z])(?=.*[a-z]))(?![.\n]).*$”;

    Anybody have a simpler way? (Checking every permutation is not so elegant.)

  2. Thanxxxx a lot. itz very useful. I searched many pages for it.. but i got here only.

  3. thank u. it used me a lot and i prepared 2 regular expressions.

    1. password must be at least 8 characters long and include at least one Special Character, one Number, and one Capital letter.

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

    2.password should be 6-15 characters and include at lease one Special Character, one Number OR one Capital Letter

    Its my expression :
    ^.*(?=^.{6,15}$)(?=.*[a-z])((?=.*\d)|(?=.*[A-Z])|(?=.*[\W])).*$

  4. Very nice… Just what I need

  5. Fantastic!! Helped a lot! Thanq Aziz.

  6. * Password length should be greater than 8 and less than 15 characters.
    * Password should contain at least one digit [0-9], one alphabet [A-Z] [a-z] and one special character such as [@#&*!].

    (?=^.{8,15}$)(?=.*\d)(?=.*\W+)(?![.\n])(?=.*[a-zA-Z]).*$

  7. all of the regular expression that is described here take white space as input and give no error on submit

  8. Now this is the modified one and it works

    (?=^.{5,15}$)(?=.[a-zA-Z\d])(?=.*[!@#$%^&*_+~])(?!.*\s).*$

  9. Thanks a lot Hemant for pointing out the issue and providing a solution for it as well.

  10. Hi

    I need to create a reg exp with the following characteristics: alphanumeric, at least one number and no more and no less than 15 characters.

    Please help!

  11. Hey Leo,
    You can take Hemant’s expression and modify it as such:

    (?=^.{15}$)(?=.[a-zA-Z\d])(?=.*[!@#$%^&*_+~])(?!.*\s).*$

    This will allow you to validate the input to make sure that the length is 15 characters
    (?=^.{15}$)

    And then valide that it contains the alpha(upper/lower) and special characters
    (?=.[a-zA-Z\d])(?=.*[!@#$%^&*_+~])(?!.*\s).*$
    or
    (?=.[\w\d])(?=.*[\S])(?!.*\s).*$

  12. Very informative and helpful. Most of the people try to use dictionary words like name or birth date, which is easy to break.

Leave a Reply