密码正则表达式(至少 2 位数字和 1 个特殊字符,最小长度 8) [英] Regular expression for password (at least 2 digits and one special character and minimum length 8)

查看:45
本文介绍了密码正则表达式(至少 2 位数字和 1 个特殊字符,最小长度 8)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找接受至少两位数字和一个特殊字符且最小密码长度为 8 的正则表达式.到目前为止,我已经完成了以下操作:[0-9a-zA-Z!@#$%0-9]*[!@#$%0-9]+[0-9a-zA-Z!@#$%0-9]*

I have been searching for regular expression which accepts at least two digits and one special character and minimum password length is 8. So far I have done the following: [0-9a-zA-Z!@#$%0-9]*[!@#$%0-9]+[0-9a-zA-Z!@#$%0-9]*

推荐答案

这样的事情应该可以解决问题.

Something like this should do the trick.

^(?=(.*\d){2})(?=.*[a-zA-Z])(?=.*[!@#$%])[0-9a-zA-Z!@#$%]{8,}

(?=(.*\d){2}) - uses lookahead (?=) and says the password must contain at least 2 digits

(?=.*[a-zA-Z]) - uses lookahead and says the password must contain an alpha

(?=.*[!@#$%]) - uses lookahead and says the password must contain 1 or more special characters which are defined

[0-9a-zA-Z!@#$%] - dictates the allowed characters

{8,} - says the password must be at least 8 characters long

它可能需要一些调整,例如准确指定您需要哪些特殊字符,但它应该可以解决问题.

It might need a little tweaking e.g. specifying exactly which special characters you need but it should do the trick.

这篇关于密码正则表达式(至少 2 位数字和 1 个特殊字符,最小长度 8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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