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

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

    Testing this one in 2020:

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

    Verify yourself

    const regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
    const str = `some12*Nuts`;
    let m;
    
    if ((m = regex.exec(str)) !== null) {
        // The result can be accessed through the `m`-variable.
        m.forEach((match, groupIndex) => {
            console.log(`Found match, group ${groupIndex}: ${match}`);
        });
    }

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

    I've found many problems here, so I made my own.

    Here it is in all it's glory, with tests:

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

    https://regex101.com/r/DCRR65/4/tests

    Things to look out for:

    1. doesn't use \w because that includes _, which I'm testing for.
    2. I've had lots of troubles matching symbols, without matching the end of the line.
    3. Doesn't specify symbols specifically, this is also because different locales may have different symbols on their keyboards that they may want to use.
    0 讨论(0)
  • 2020-11-21 05:03
    (?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+-]).{6}
    
    0 讨论(0)
  • 2020-11-21 05:04

    Demo:

    function password_check() {
      pass = document.getElementById("password").value;
      console.log(pass);
      regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
      if (regex.exec(pass) == null) {
        alert('invalid password!')
      }
      else {
        console.log("valid");
      }
    }
    <input type="text" id="password" value="Sample@1">
    <input type="button" id="submit" onclick="password_check()" value="submit">

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

    Just we can do this by using HTML5.

    Use below code in pattern attribute,

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

    It will work perfectly.

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

    In Java/Android, to test a password with at least one number, one letter, one special character in following pattern:

    "^(?=.*[A-Za-z])(?=.*\\d)(?=.*[$@$!%*#?&])[A-Za-z\\d$@$!%*#?&]{8,}$"
    
    0 讨论(0)
提交回复
热议问题