Create preg_match for password validation allowing (!@#$%)(为密码验证创建 preg_match 允许 (!@#$%))
问题描述
我想创建一个 preg_match 函数来验证我的密码,但我不确定如何编写它以允许使用以下特殊字符:!@#$%.
I would like to create a preg_match function to validate my passowrds, but I'm not sure how to write it to allow the following special characters to be used: !@#$%.
if(!preg_match(?????)$/', $password))
这是我想在正则表达式中使用的密码规则:
Here are my password rules that I want to work into the regex:
- 可能包含字母和数字
- 必须至少包含 1 个数字和 1 个字母
- 可能包含以下任何字符:
!@#$% - 必须为 8-12 个字符
感谢您提供的任何帮助.
Thank you for any help you can offer.
推荐答案
我觉得应该是这样的:
if(!preg_match('/^(?=.*d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{8,12}$/', $password)) {
echo 'the password does not meet the requirements!';
}
开始之间 -> ^
并结束 -> $
字符串中必须至少有一个数字 -> (?=.*d)
和至少一个字母 -> (?=.*[A-Za-z])
它必须是数字、字母或以下之一:!@#$% -> [0-9A-Za-z!@#$%]
并且必须有 8-12 个字符 -> {8,12}
正如 user557846 对您的问题的评论,我还建议您允许更多字符,我通常(如果我使用最大值)至少需要 50 个 :)
As user557846 commented to your question, I would also suggest you to allow more characters, I usually (if i use a maximum) take at least 50 :)
顺便说一句,您可能想看看 这个正则表达式教程
btw, you might want to take a look at this regex tutorial
这篇关于为密码验证创建 preg_match 允许 (!@#$%)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为密码验证创建 preg_match 允许 (!@#$%)
基础教程推荐
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何替换eregi() 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
