密码PHP的正则表达式 [英] Regex for password PHP

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

问题描述

我在线找到了一个脚本,并且该脚本在JavaScript中具有密码正则表达式.我仍然想使用它,但是为了提高安全性,我也想使用PHP来验证我的密码,但是我对正则表达式没用.

I found a script online and it has a password regex in JavaScript. I still want to use it, but for more security I want to use PHP to validate my password too but I'm useless with regex.

要求:

  • 至少必须包含8个字符
  • 必须包含至少1个数字
  • 必须至少包含一个大写字符
  • 必须至少包含一个小写字符

如何构造正则表达式字符串来满足这些要求?

How can I construct a regex string to meet these requirements?

推荐答案

我发现在一个大的正则表达式中执行该操作有点像代码维护的噩梦.将其拆分起来很容易让其他人查看您的代码,并且还可以提供更具体的错误消息.

I find that doing it in one big regex is a bit of a code maintenance nightmare. Splitting it up is far easier to figure out for someone else looking at your code, and it allows you to give more specific error messages as well.

$uppercase = preg_match('@[A-Z]@', $password);
$lowercase = preg_match('@[a-z]@', $password);
$number    = preg_match('@[0-9]@', $password);

if(!$uppercase || !$lowercase || !$number || strlen($password) < 8) {
  // tell the user something went wrong
}

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

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