javascript正则表达式

javascript正则表达式

本文介绍了javascript正则表达式,包含至少8个字符,1个数字,1个大写和1个小写的密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


  • 包含至少8个字符

  • 包含至少1个数字

  • 包含至少1个小写字符(az)

  • 包含至少1个大写字母(AZ)

  • 仅包含0-9a-zA-Z

  • 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

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

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

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

和...

$(".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");
    }

});

任何想法?

推荐答案

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

Your regular expression should look like:

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

以下是解释:

/^
  (?=.*\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
$/

这篇关于javascript正则表达式,包含至少8个字符,1个数字,1个大写和1个小写的密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 22:25