本文介绍了ASP.NET普通防爆pression验证(密码强度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下前pression验证控件:

I have a validation control that has the following expression:

(?=(.*\\d.*){2,})(?=(.*\\w.*){2,})(?=(.*\\W.*){1,}).{8,}

这是至少有 2位数 2字母字符 1非字母数字 8个字符的最小。不幸的是这似乎并不为跨浏览器兼容。

That's a password with at least 2 digits, 2 alpha characters, 1 non-alphanumeric and 8 character minimum. Unfortunately this doesn't seem to be cross-browser compliant.

此验证工作完全在Firefox,但它在Internet Explorer中没有。

This validation works perfectly in Firefox, but it does not in Internet Explorer.

您的每一个答案结果的组合:

A combination of each of your answers results in:

var format = "^(?=.{" + minLength + ",})" +
    (minAlpha > 0 ? "(?=(.*[A-Za-z].*){" + minAlpha + ",})" : "") +
    (minNum > 0 ? "(?=(.*[0-9].*){" + minNum + ",})" : "") +
    (minNonAlpha > 0 ? "(?=(.*\\W.*){" + minNonAlpha + ",})" : "") + ".*$";

EX: "^(?=.{x,})(?=(.*[A-Za-z].*){y,})(?=(.*[0-9].*){z,})(?=(.*\W.*){a,}).*$"

重要件具有(?{X,})为长度第一

推荐答案

(?=(。* \\ W。*){0,})不为0的非-字母数字字符。它的至少0 的非字母数字字符。如果你想要的密码,不包含任何非字母数字字符,你可以做任何(?!。* \\ W)(?= \\ w * $)

(?=(.*\W.*){0,}) is not 0 non-alphanumeric characters. It is at least 0 non-alphanumeric characters. If you wanted the password to not contain any non-alphanumeric characters you could do either (?!.*\W) or (?=\w*$).

有一个简单的解决方案将是跳过 \\ W 前瞻,并使用 \\ W {8} 而不是 {8}

A simpler solution would be to skip the \W look-ahead, and use \w{8,} instead of .{8,}.

此外, \\ W 包括 \\ D 。如果你想只阿尔法你可以做任何 [^ \\ W \\ D] [A-ZA-Z]

Also, \w includes \d. If you wanted just the alpha you could do either [^\W\d] or [A-Za-z].

/^(?=(?:.*?\d){2})(?=(?:.*?[A-Za-z]){2})\w{8,}$/

这将验证密码至少包含两位 两位阿尔法,是是在至少8个字符长,并包含仅字母数字字符(包括下划线)。

This would validate the password to contain at least two digits, two alphas, be at least 8 characters long, and contain only alpha-numeric characters (including underscore).


  • \\ W = [A-ZA-Z0-9 _]

  • \\ D = [0-9]

  • \\ S = [\\ t \\ n \\ r \\ F \\ V]

  • \w = [A-Za-z0-9_]
  • \d = [0-9]
  • \s = [ \t\n\r\f\v]

编辑:
在所有浏览器使用这个你可能需要做这样的事情:

To use this in all browsers you probably need to do something like this:

var re = new RegExp("^(?=(?:.*?\\d){2})(?=(?:.*?[A-Za-z]){2})\\w{8,}$");
if (re.test(password)) { /* ok */ }

EDIT2:近期讨论的更新我的全部答案几乎无效。 ^^ ;;

The recent update in the question almost invalidates my whole answer. ^^;;

您应该仍然能够使用JavaScript code在最后,如果你与你有什么替代原先的格局。

You should still be able to use the JavaScript code in the end, if you replace the pattern with what you had originally.

EDIT3:确定。现在我明白你的意思。

OK. Now I see what you mean.

/^(?=.*[a-z].*[a-z])(?=.*[0-9].*[0-9]).{3,}/.test("password123") // matches
/^(?=.*[a-z].*[a-z])(?=.*[0-9].*[0-9]).{4,}/.test("password123") // does not match
/^(?=.*[a-z].*[a-z]).{4,}/.test("password123")                   // matches

这似乎(?=)是不是真的零宽度在Internet Explorer中。

It seems (?= ) isn't really zero-width in Internet Explorer.

http://development.thatoneplace.net/2008/05/bug-discovered-in-internet-explorer-7.html

Edit4:更多阅读:http://blog.stevenlevithan.com/archives/regex-lookahead-bug

我认为这能解决你的问题:

I think this can solve your problem:

/^(?=.{8,}$)(?=(?:.*?\d){2})(?=(?:.*?[A-Za-z]){2})(?=(?:.*?\W){1})/
new RegExp("^(?=.{8,}$)(?=(?:.*?\\d){2})(?=(?:.*?[A-Za-z]){2})(?=(?:.*?\\W){1})")

(?= {8} $)需要是第一位的。

这篇关于ASP.NET普通防爆pression验证(密码强度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:13