创建preg_match进行密码验证,允许(!@#$%) [英] Create preg_match for password validation allowing (!@#$%)

查看:122
本文介绍了创建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个字符
  • May contain letter and numbers
  • Must contain at least 1 number and 1 letter
  • May contain any of these characters: !@#$%
  • Must be 8-12 characters

感谢您提供的任何帮助.

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}

Between start -> ^
And end -> $
of the string there has to be at least one number -> (?=.*\d)
and at least one letter -> (?=.*[A-Za-z])
and it has to be a number, a letter or one of the following: !@#$% -> [0-9A-Za-z!@#$%]
and there have to be 8-12 characters -> {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 :)

顺便说一句,您可能想看看此regex教程

btw, you might want to take a look at this regex tutorial

这篇关于创建preg_match进行密码验证,允许(!@#$%)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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