I'm validating a password for complexity in an ASP.NET MVC3 app. My current requirements are that it must contain at least one upper case letter, one lower case letter, one digit and no more than three repeated characters. I'd like to generalise those numbers though, and also add a condition for non-alphanumeric characters.

我正在验证ASP.NET MVC3应用程序中复杂性的密码。我目前的要求是它必须包含至少一个大写字母,一个小写字母,一个数字和不超过三个重复字符。我想概括这些数字,并为非字母数字字符添加条件。

At present, I'm validating server-side only, so I'm able to call Regex.IsMatch multiple times using one regex for each condition. I want to be able to validate client-side too though. because unobtrusive jQuery validation will only allow one regex, I need to combine all five conditions into a single pattern.

目前,我只验证服务器端,因此我可以使用一个正则表达式为每个条件多次调用Regex.IsMatch。我希望能够验证客户端。因为不显眼的jQuery验证只允许一个正则表达式,我需要将所有五个条件组合成一个模式。

I don't know much when it comes to regular expressions but I've been doing a bit of reading recently. I may be missing something simple but I can't find a way to AND multiple patterns together the way a | will OR them.

我对正则表达式的了解不多,但最近我一直在做一些阅读。我可能会遗漏一些简单的东西,但我无法找到一种方式将多个模式组合在一起他们会。

1 个解决方案

#1


8

You can do this (in .NET) with several lookahead assertions in a single regex:

您可以在单个正则表达式中使用多个前瞻断言执行此操作(在.NET中):

^(?=.*\p{Lu})(?:.*\p{Ll})(?=.*\d)(?=.*\W)(?!.*(.).*\1.*\1)

will match if all conditions are true.

如果所有条件都为真,则匹配。

^                  # Match the start of the string
(?=.*\p{Lu})       # True if there is at least one uppercase letter ahead
(?=.*\p{Ll})       # True if there is at least one lowercase letter ahead
(?=.*\d)           # True if there is at least one digit ahead
(?=.*\W)           # True if there is at least one non-alnum character ahead
(?!.*(.).*\1.*\1)  # True if there is no character repeated twice ahead

Note that the match is not going to consume any characters of the string - if you want the match operation to return the string you're matching against, add .* at the end of the regex.

请注意,匹配不会消耗字符串的任何字符 - 如果您希望匹配操作返回您匹配的字符串,请在正则表达式的末尾添加。*。

In JavaScript, you can't use Unicode character properties. So instead you could use

在JavaScript中,您不能使用Unicode字符属性。所以你可以使用

^(?=.*[A-Z])(?:.*[a-z])(?=.*\d)(?=.*\W)(?!.*(.).*\1.*\1)

which will of course only use ASCII letters for validation. If that's OK for you, fine. You could go and augment the character classes like [A-ZÄÖÜÀÈÌÒÙÁÉÍÓÚ] etc. etc. but you would probably never be complete with this. On the server side, if you want the validation to yield the same result, you'd have to specify RegexOptions.ECMAScript so the .NET regex engine behaves like the JavaScript engine (thanks Alan Moore for noticing!).

当然只会使用ASCII字母进行验证。如果这对你没问题,那很好。你可以去增加像[A-ZÄÖÜÀÈÌÒÙÁÉÍÓÚ]等人物类别,但你可能永远不会完成这个。在服务器端,如果您希望验证产生相同的结果,则必须指定RegexOptions.ECMAScript,以便.NET正则表达式引擎的行为类似于JavaScript引擎(感谢Alan Moore注意!)。

更多相关文章

  1. 带有无线电的JavaScript条件字段不起作用
  2. Javascript正则表达式应该允许空和非空字符串,但它不会
  3. 获取拆分字符串数组的最后一个元素
  4. “错误:路径必须是字符串”(v5.10.0)
  5. javascript 截取字符串方法汇总
  6. JavaScript String(字符串对象)
  7. JavaScript正则表达式定义字符集
  8. JS将字符串转换为数组
  9. 在变量名是字符串时声明变量?

随机推荐

  1. Android实现启动页停留几秒然后自动跳转
  2. android 硬解码 是否支持
  3. Android实现轮播图效果
  4. sqlite3 表结构查询
  5. android获取屏幕大小
  6. Fresco使用的注意事项
  7. Android(安卓)ApiDemos 系列解析【View-I
  8. 关于安卓开发实现拖动条和星级评论
  9. android 瀑布流
  10. 腾讯代理下载Android SDK