【问题标题】:Password Validation with Sequential Letters and Numbers - RegEx使用连续字母和数字验证密码 - RegEx
【发布时间】:2015-09-29 19:34:49
【问题描述】:

为了让客户帐户更加安全,精心设计的密码是一种很好的做法。这是我用于密码验证的正则表达式字符串。

/^(?=.*[0-9])(?!.*?\d{3})(?=.*[a-zA-Z])(?!.*?[a-zA-Z]{3})(?=.*[~!@#$%^&*()+-?])([a-zA-Z0-9~!@#$%^&*()+-?]{8,})$/

表示:

  • 8 个或更多字符。
  • 大写字母 A-Z
  • 小写字母 a-z
  • 特殊字符~!@#$%^&*()+-?
  • 这个正则表达式函数是什么?:不得包含最多 3 个连续的字母和/或数字。

数字和/或字母顺序为 3 或更多是不合适的。

例子:

不正常 = efg123!$, abcd567%, xyz789^&, #hijk23456
正常 = ryiiu562@, erty745#, gjnfl45566^

谢谢

【问题讨论】:

  • 没有简单的正则表达式。
  • 我认为你对你的用户过于苛刻了。用户应对其密码负责。即使您尝试在密码中禁止12345abcde,您也无法阻止他们可以想象的所有愚蠢序列。如果您想要更强的密码,请增加最小大小(或禁止美国用户使用圣经节选)。
  • 使用第三方库怎么样?可能类似于 dropbox 的 zxcvnb 并测试密码分数...
  • 别忘了禁止序列are you lonesome tonight
  • 您的正则表达式有错误。您的两个字符类中有+-?,它定义了一个与[+,\-./0123456789:;<=>?] 等效的范围。我很确定这不是你的意思。

标签: javascript regex password-protection


【解决方案1】:

据我所知,没有办法使用 RegEx,但这是一种幼稚的函数式方法。

首先,遍历字符串并将每个字符与接下来的两个字符进行比较,方法是将 +1 和 +2 添加到当前索引并进行适当的比较。

其次,再次循环遍历字符串并比较检查下两个字符与当前字符是否是连续的。

如果两个循环都找不到连续字符,则函数返回true,否则返回false。

前四个返回false(失败),后三个返回true(通过)。

function test(s) {
    // Check for sequential numerical characters
    for(var i in s) 
        if (+s[+i+1] == +s[i]+1 && 
            +s[+i+2] == +s[i]+2) return false;
    // Check for sequential alphabetical characters
    for(var i in s) 
        if (String.fromCharCode(s.charCodeAt(i)+1) == s[+i+1] && 
            String.fromCharCode(s.charCodeAt(i)+2) == s[+i+2]) return false;
    return true;
}

// For demo purposes only
var tests = [
    'efg123!$',
    'abcd567%',
    'xyz789^&',
    '#hijk23456',
    'ryiiu562@',
    'erty745#',
    'gjnfl45566^'
], sep = '\t\u2192 ', out = ['Fail','Pass'], eol = '<br>';
document.write('<pre>');
for(var i in tests) document.write(tests[i] + sep + out[+test(tests[i])] + eol);
document.write('</pre>');

【讨论】:

  • 我知道这有点旧,但非常需要,但是,我还需要它来匹配降序和升序。它只是负数而不是正数吗?
【解决方案2】:

您可以通过循环字符并使用charCodeAt 字符串方法来获得类似于以下的功能。

注意:这也是针对以下链接中提出的问题。

string validation for 3 or more consecutive sequential alphanumeric characters in javascript

function validate() {
  var pwd = document.getElementById('password').value;
  var isValid = checkPassword(pwd);
  var elm = document.getElementById('result');
  elm.innerHTML = isValid ? 'Valid' : 'Invalid';
  elm.style.color = isValid ? 'green' : 'red';
}

function checkPassword(s) {
    
    if(s) {
       var test = (x) => !isNaN(x);
       var check = (x, y, i) => x + i === y;
    
       for(var i = 0; i < s.length - 2; i++) {
         if(test(s[i])) {
            if(test(s[i + 1]) && test(s[i + 2])) {
              if(check(Number(s[i]),Number(s[i + 1]), 1) &&
                check(Number(s[i]), Number(s[i + 2]), 2)) {
                return false;
              }
            }
         } else if(!test(s[i + 1]) && !test(s[i + 2])) {
            if(check(s.charCodeAt(i), s.charCodeAt(i + 1), 1) &&
                check(s.charCodeAt(i), s.charCodeAt(i + 2), 2)) {
                return false;
              }
         }
       }
      
    }
    
    return true;
}

document.getElementById('buttonToValidate').click();
<input type="text" id="password" value="efg123!$" /> 
<input type="button" id="buttonToValidate" value="Check" onclick="validate()" />
<span id="result" />

【讨论】:

    【解决方案3】:

    根据上面的答案添加降序检查;

    function test(s) {
        // Check for sequential numerical characters
        for(var i in s){
          if (+s[+i+1] == +s[i]+1 ) return false;
          if (+s[+i+1]+1 == +s[i] ) return false;
        }
    
        // Check for sequential alphabetical characters
        for(var i in s){
          if (String.fromCharCode(s.charCodeAt(i)+1) == s[+i+1]) return false;
          if (String.fromCharCode(s.charCodeAt(i)-1) == s[+i+1]) return false;
        }
    
        return true;
      }
    
    // For demo purposes only
    var tests = [
        'efg123!$',
        'abcd567%',
        'xyz789^&',
        '#hijk23456',
        'ryiiu562@',
        'erty745#',
        'gjnfl45566^',
        '2a3b5c6',
        'mortz',
        '1357911'
    ], sep = '\t\u2192 ', out = ['Fail','Pass'], eol = '<br>';
    document.write('<pre>');
    for(var i in tests) document.write(tests[i] + sep + out[+test(tests[i])] + eol);
    document.write('</pre>');

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-13
      • 2016-02-02
      • 2012-01-03
      • 2011-09-06
      相关资源
      最近更新 更多