包含至少 8 个字符、1 个数字、1 个大写和 1 个小写的密码的 javascript 正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14850553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 18:08:29  来源:igfitidea点击:

javascript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase

javascriptjqueryregexvalidation

提问by Tom

I am trying to write a regular expression to validate a password which must meet the following criteria:

我正在尝试编写一个正则表达式来验证必须满足以下条件的密码:

  • Contain at least 8 characters
  • contain at least 1 number
  • contain at least 1 lowercase character (a-z)
  • contain at least 1 uppercase character (A-Z)
  • contains only 0-9a-zA-Z
  • 至少包含 8 个字符
  • 至少包含 1 个数字
  • 包含至少 1 个小写字符 (az)
  • 包含至少 1 个大写字符 (AZ)
  • 仅包含 0-9a-zA-Z

I tried the following but it doesn't seem to work.

我尝试了以下但它似乎不起作用。

http://jsfiddle.net/many_tentacles/Hzuc9/

http://jsfiddle.net/many_tentacles/Hzuc9/

<input type='button' value='click' class='buttonClick' />
<input type='text' />
<div></div>

and...

和...

$(".buttonClick").click(function () {

    if ($("input[type=text]").filter(function () {
        return this.value.match(/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])([a-zA-Z0-9]{8})$/);
    })) {
        $("div").text("pass");
    } else {
        $("div").text("fail");
    }

});

Any ideas?

有任何想法吗?

回答by Minko Gechev

Your regular expression should look like:

您的正则表达式应如下所示:

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

Here is an explanation:

这是一个解释:

/^
  (?=.*\d)          // should contain at least one digit
  (?=.*[a-z])       // should contain at least one lower case
  (?=.*[A-Z])       // should contain at least one upper case
  [a-zA-Z0-9]{8,}   // should contain at least 8 from the mentioned characters
$/

回答by Anthony Grist

Using individual regular expressions to test the different parts would be considerably easier than trying to get one single regular expression to cover all of them. It also makes it easier to add or remove validation criteria.

使用单独的正则表达式来测试不同的部分比试图用一个正则表达式来覆盖所有部分要容易得多。它还可以更轻松地添加或删除验证标准。

Note, also, that your usage of .filter()was incorrect; it will always return a jQuery object (which is considered truthy in JavaScript). Personally, I'd use an .each()loop to iterate over all of the inputs, and report individual pass/fail statuses. Something like the below:

另请注意,您的用法.filter()不正确;它将始终返回一个 jQuery 对象(在 JavaScript 中被认为是真实的)。就个人而言,我会使用.each()循环来遍历所有输入,并报告各个通过/失败状态。类似于以下内容:

$(".buttonClick").click(function () {

    $("input[type=text]").each(function () {
        var validated =  true;
        if(this.value.length < 8)
            validated = false;
        if(!/\d/.test(this.value))
            validated = false;
        if(!/[a-z]/.test(this.value))
            validated = false;
        if(!/[A-Z]/.test(this.value))
            validated = false;
        if(/[^0-9a-zA-Z]/.test(this.value))
            validated = false;
        $('div').text(validated ? "pass" : "fail");
        // use DOM traversal to select the correct div for this input above
    });
});

Working demo

工作演示

回答by nbrooks

At least 8 = {8,}:

至少 8 = {8,}

str.match(/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])([a-zA-Z0-9]{8,})$/)

回答by Tim Pietzcker

Your regex only allows exactly 8 characters. Use {8,}to specify eight or more instead of {8}.

您的正则表达式只允许 8 个字符。使用{8,}指定八个或更多,而不是{8}

But why would you limit the allowed character range for your passwords? 8-character alphanumeric passwords can be bruteforced by my phonewithin minutes.

但是为什么要限制密码的允许字符范围呢?我的手机可以在几分钟内暴力破解 8 个字符的字母数字密码。