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

About these ads

27 thoughts on “Regular Expressions for strong password in asp.net

  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. 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])).*$

  3. * 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]).*$

    • This comment is old but, it is good. Another “but” for me it failed. Where?? If you put a password like this: a#1B2c
      it fail if you put one vowel in the first position without a vowel in the second like this: ab#1B2c
      Another question is if you put a unlisted character like: ab#1ºB2c it validate de against the pattern.
      Solution that I found: (?=^.{5,15}$)^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*_+~])(?!./s*)[a-zA-Z0-9!@#$%^&*_+~]*$

  4. 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!

  5. 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).*$

  6. What about a regular expression wherer the password must be at least 15 characters and contain at least 2 each of uppercase letters, lowercase letters, numbers and special characters?

  7. I need regular expressin fot the following condition
    1.Firtst character must be a capital
    2.Atleast 6 character
    3.atleast 1 special character
    4.atleast 1 number

  8. i need regular expression which specified as below:
    1) Combination of upper an lower expression
    2) Begin and End of password must be char not integer
    3) Minimum size is 8 digit and maximum size is 12 digits

    Thank you so much for help!

  9. Should you not drive your company, you may be driven out of business.
    There won’t be any tips for success. It is the result of preparation, work, and gaining knowledge from failure.

  10. Hi Everyone,
    I’m looking for regular expression to validate the following.
    • Password length must be from 8 to 32 characters
    • Excluded Character ^&*(
    • Require at least one non-alphabetic character

    Thanks in advance.

  11. What does this mean…..

    your password must be between 8 and 15 characters and atleast one digit with one alphabetic character with no special characters?

  12. i want validation expression for textbox that should accept atleast 1 uppercase,1 lowercase,1 number and 1 special character..and password length should be 8 to 15..and one more thing..password should not accept space..for example when we click on spacebar it is taking as a special character..i want to exclude space

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s