使用顺序字母和数字进行密码验证 - RegEx [英] Password Validation with Sequential Letters and Numbers - RegEx

查看:217
本文介绍了使用顺序字母和数字进行密码验证 - RegEx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

To have customer accounts more secure, a well crafted password is good practice. This is my Regular Expression string for password validation.

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

代表:


  • 8个或更多字符。

  • 大写字母AZ

  • 小写字母az

  • 特殊字符〜!@#$%^& *()+ - ?

  • 此正则表达式功能是什么?:最多不得包含3个连续字母和/或数字。

  • 8 or more characters.
  • Uppercase letter A-Z
  • Lowercase letters a-z
  • Special characters ~!@#$%^&*()+-?
  • What is this Regular Expression function for this?: Must not contain up to 3 sequential letters and/or numbers.

按顺序排列3个或更多的数字和/或字母是不行的。

Having numbers and/or letters in order 3 or more sequential is not OK.

示例:

不行= efg123!$,abcd567%,xyz789 ^&,#hijk23456

确定 = ryiiu562 @,erty745#,gjnfl45566 ^

Not OK = efg123!$, abcd567%, xyz789^&, #hijk23456
OK = ryiiu562@, erty745#, gjnfl45566^

谢谢

推荐答案

我无法使用我所知道的RegEx,但这是一种天真的功能方法。

There's no way using RegEx that I know of, but here is a naive functional approach.

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

First, loop through the string and compare each character against the next two characters by adding +1 and +2 to the current index and comparing appropriately.

其次,再次遍历字符串并比较检查当前字符的后两个字符,看它们是否是连续的。

Second, loop through the string again and compare checks the next two characters against the current character to see if they are sequential.

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

If both loops fail to find sequential characters, the function returns true, otherwise it returns false.

前四个返回false(失败),而后三个返回true(pass)。

The first four return false (fail), while the last three return true (pass).

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>');

这篇关于使用顺序字母和数字进行密码验证 - RegEx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆