创建正则表达式以检查强密码 [英] Creating a regex to check for a strong password

查看:124
本文介绍了创建正则表达式以检查强密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个用于检查字母数字的正则表达式.

Say I have a regex that checks for alphanumeric.

我现在想创建另一个正则表达式,该正则表达式检查密码中的至少1个数字. 我想检查它是否至少包含1个非字母数字字符(字母或数字以外的其他东西).

I now want to create another regex that checks for at least 1 number in the password. And I want to check if it has at least 1 non-alphanumeric character (somethign other than a letter or number).

我应该只给每一个单独的电话打电话,如果失败则返回false或有办法将它们组合为1个电话吗?

Should I just call each one seperatley, and if one fails return false or is there a way to combine them into 1 call?

推荐答案

具体取决于您使用的标准,但是最好通过字符串进行一次遍历并根据设置多个标志来更好您已经看到:

Depends on exactly what criteria you're using, but it would probably be better to do a single pass through the string and set a number of flags based on what you've seen:

  • hasDigit
  • hasAlpha
  • hasSymbol

然后最后使用它们来确定密码是否足够复杂.

Then use these at the end to decide if the password is complex enough.

更好:

如lexu所建议的那样,使用计数代替标志将提供更大的灵活性.

As lexu suggests, using counts instead of flags would allow greater flexibility.

这篇关于创建正则表达式以检查强密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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