Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters

前端 未结 30 3628
伪装坚强ぢ
伪装坚强ぢ 2020-11-21 04:28

I want a regular expression to check that:

A password contains at least eight characters, including at least one number and includes both lower and uppercase letter

相关标签:
30条回答
  • 2020-11-21 05:16

    I've actually just copied the first answer here and turned it into a more ux-convenient regex which needs one upper, one lower and at least 8 chars but accepts everything "in between".

    This one is an example-regex which requires

    1. at least 8 characters length
    2. at least one lowercase letter
    3. at least one uppercase letter

    IMPORTANT: This regex will also except all other characters e.g. numbers, special characters like $,#,! etc. - as long as the rules 1. to 3. match the input string

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

    Mind the "." alomst at the end of the regex. This will match almost any (and afaik any readable) character

    0 讨论(0)
  • 2020-11-21 05:17

    What about considering the following regex solution:

    ^(?=.*[\w])(?=.*[\W])[\w\W]{8,}$

    Which validates the following:

    1. At least one lowercase
    2. At least one uppercase
    3. At least one digit
    4. At least one special character
    5. At least it should have 8 characters long.

    Check it out working at the following link https://regex101.com/r/qPmC06/4/

    0 讨论(0)
  • 2020-11-21 05:18

    I would reply to Peter Mortensen, but I don't have enough reputation.

    His expressions are perfect for each of the specified minimum requirements. The problem with his expressions that don't require special characters is that they also don't ALLOW special characters, so they also enforce maximum requirements, which I don't believe the OP requested. Normally you want to allow your users to make their password as strong as they want; why restrict strong passwords?

    So, his "minimum eight characters, at least one letter and one number" expression:

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

    achieves the minimum requirement, but the remaining characters can only be letter and numbers. To allow (but not require) special characters, you should use something like:

    ^(?=.*[A-Za-z])(?=.*\d).{8,}$ to allow any characters

    or

    ^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d$@$!%*#?&]{8,}$ to allow specific special characters

    Likewise, "minimum eight characters, at least one uppercase letter, one lowercase letter and one number:"

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

    meets that minimum requirement, but only allows letters and numbers. Use:

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

    or

    ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[A-Za-z\d$@$!%*?&]{8,} to allow specific special characters.

    0 讨论(0)
  • 2020-11-21 05:20

    I had some difficulty following the most popular answer for my circumstances. For example, my validation was failing with characters such as ; or [. I was not interested in white-listing my special characters, so I instead leveraged [^\w\s] as a test - simply put - match non word characters (including numeric) and non white space characters. To summarize, here is what worked for me...

    • at least 8 characters
    • at least 1 numeric character
    • at least 1 lowercase letter
    • at least 1 uppercase letter
    • at least 1 special character

    /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\w\s]).{8,}$/
    

    JSFiddle Link - simple demo covering various cases

    0 讨论(0)
  • 2020-11-21 05:20

    @ClasG has already suggested:

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

    but it does not accept _(underscore) as a special character (eg. Aa12345_).

    An improved one is:

    ^(?=\S*[a-z])(?=\S*[A-Z])(?=\S*\d)(?=\S*([^\w\s]|[_]))\S{8,}$
    
    0 讨论(0)
  • 2020-11-21 05:21

    This worked for me:

    ^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@$!%*?&])([a-zA-Z0-9@$!%*?&]{8,})$
    
    • At least 8 characters long;
    • One lowercase, one uppercase, one number and one special character;
    • No whitespaces.
    0 讨论(0)
提交回复
热议问题