普通防爆pression的密码验证 [英] Regular Expression for password validation

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

问题描述

我目前使用这个普通的前pression检查一个字符串符合几个条件。

I currently use this regular expression to check if a string conforms to a few conditions.

的条件是 字符串必须介于8到15个字符长。 字符串必须包含至少一个数字。 字符串必须至少包含一个大写字母。 字符串必须至少包含一个小写字母。

The conditions are string must be between 8 and 15 characters long. string must contain at least one number. string must contain at least one uppercase letter. string must contain at least one lowercase letter.

(?!^ [0-9] * $)(?!^ [A-ZA-Z] * $)^([A-ZA-Z0-9] {8,15 })$

它适用于在大多数情况下,但它不容许特殊字符。任何帮助修改此正则表达式,允许特殊字符是多少AP preciated。

It works for the most part, but it does not allow special character. Any help modifying this regex to allow special character is much appreciated.

感谢

推荐答案

似乎有很多混乱在这里。我认为到目前为止,这些问题的答案不正确执行1+编号/ 1 +小写/ 1 +大写规则,也就是说,例如 ABC123密码 123XYZ AB *放大器; ^#仍然会被接受。 preventing全小写,全大写,或者全数字是不够的;你必须执行的每个中的至少一个的presence

There seems to be a lot of confusion here. The answers I see so far don't correctly enforce the 1+ number/1+ lowercase/1+ uppercase rule, meaning that passwords like abc123, 123XYZ, or AB*&^# would still be accepted. Preventing all-lowercase, all-caps, or all-digits is not enough; you have to enforce the presence of at least one of each.

请尝试以下操作:

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

如果您也想要求至少一个特殊字符(这可能是一个好主意),试试这个:

If you also want to require at least one special character (which is probably a good idea), try this:

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

{8,15} 可如果你想更严格(例如,你可以将其更改为 \ S {8 ,15} 不允许空白),但请记住,这样做会降低你的密码方案的实力。

The .{8,15} can be made more restrictive if you wish (for example, you could change it to \S{8,15} to disallow whitespace), but remember that doing so will reduce the strength of your password scheme.

我测试过这种模式,它按预期工作。 测试在ReFiddle这里: http://refiddle.com/110

I've tested this pattern and it works as expected. Tested on ReFiddle here: http://refiddle.com/110

编辑:一个小提醒,在简单办法做到这一点是有3个独立的正则表达式和字符串的长度财产。它也更容易阅读和维护,所以这样做的,如果你有选择。如果这是在标记验证规则,不过,你可能坚持使用一个单一的正则表达式。

One small note, the easiest way to do this is with 3 separate regexes and the string's Length property. It's also easier to read and maintain, so do it that way if you have the option. If this is for validation rules in markup, though, you're probably stuck with a single regex.

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

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