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

前端 未结 30 3499
伪装坚强ぢ
伪装坚强ぢ 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:05

    According to your need this pattern should work just fine. Try this,

    ^(?=(.*\d){1})(.*\S)(?=.*[a-zA-Z\S])[0-9a-zA-Z\S]{8,}
    

    Just create a string variable, assign the pattern, and create a boolean method which returns true if the pattern is correct, else false.

    Sample:

    String pattern = "^(?=(.*\d){1})(.*\S)(?=.*[a-zA-Z\S])[0-9a-zA-Z\S]{8,}";
    String password_string = "Type the password here"
    
    private boolean isValidPassword(String password_string) {
        return password_string.matches(Constants.passwordPattern);
    }
    
    0 讨论(0)
  • 2020-11-21 05:06

    A more "generic" version(?), allowing none English letters as special characters.

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

    var pwdList = [
        '@@V4-\3Z`zTzM{>k',
        '12qw!"QW12',
        '123qweASD!"#',
        '1qA!"#$%&',
        'Günther32',
        '123456789',
        'qweASD123',
        'qweqQWEQWEqw',
        '12qwAS!'
      ],
      re = /^(?=\S*[a-z])(?=\S*[A-Z])(?=\S*\d)(?=\S*[^\w\s])\S{8,}$/;
      
      pwdList.forEach(function (pw) {
        document.write('<span style="color:'+ (re.test(pw) ? 'green':'red') + '">' + pw + '</span><br/>');
      });

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

    Try this one:

    1. Minimum six characters
    2. At least one uppercase character
    3. At least one lowercase character
    4. At least one special character

    Expression:

    "/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&.])[A-Za-z\d$@$!%*?&.]{6, 20}/"
    

    Optional Special Characters:

    1. At least one special character
    2. At least one number
    3. Special characters are optional
    4. Minimum six characters and maximum 16 characters

    Expression:

    "/^(?=.*\d)(?=.*[a-zA-Z]).{6,20}$/"
    

    If the min and max condition is not required then remove .{6, 16}

    • 6 is minimum character limit
    • 20 is maximum character limit
    • ?= means match expression
    0 讨论(0)
  • 2020-11-21 05:06
    ^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!@#$%^&*()_+,.\\\/;':"-]).{8,}$
        
    
    0 讨论(0)
  • 2020-11-21 05:07

    Use the following Regex to satisfy the below conditions:

    Conditions: 1] Min 1 special character.
                2] Min 1 number.
                3] Min 8 characters or More
    

    Regex: ^(?=.*\d)(?=.*[#$@!%&*?])[A-Za-z\d#$@!%&*?]{8,}$

    Can Test Online: https://regex101.com

    0 讨论(0)
  • 2020-11-21 05:08
    • Must contain at least 1 number, 1 uppercase, 1 lowercase letter and at least 8 or more characters: https://www.w3schools.com/howto/howto_js_password_validation.asp
    pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}"
    
    0 讨论(0)
提交回复
热议问题