php 为密码验证创建 preg_match 允许 (!@#$%)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11873990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create preg_match for password validation allowing (!@#$%)
提问by Mark Rummel
I would like to create a preg_matchfunction to validate my passowrds, but I'm not sure how to write it to allow the following special characters to be used: !@#$%.
我想创建一个preg_match函数来验证我的密码,但我不确定如何编写它以允许使用以下特殊字符:!@#$%.
if(!preg_match(?????)$/', $password))
Here are my password rules that I want to work into the regex:
这是我想在正则表达式中使用的密码规则:
- 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
- 可能包含字母和数字
- 必须至少包含 1 个数字和 1 个字母
- 可能包含以下任何字符:
!@#$% - 必须为 8-12 个字符
Thank you for any help you can offer.
感谢您提供的任何帮助。
回答by r3bel
I think this should look like that:
我认为这应该是这样的:
if(!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{8,12}$/', $password)) {
echo 'the password does not meet the requirements!';
}
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}
在
字符串的开始 ->^
和结束 ->$
之间必须至少有一个数字 ->(?=.*\d)
和至少一个字母 ->(?=.*[A-Za-z])
并且它必须是一个数字、一个字母或以下之一:!@#$% - >[0-9A-Za-z!@#$%]
并且必须有 8-12 个字符 ->{8,12}
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 :)
当 user557846 对您的问题发表评论时,我还建议您允许更多字符,我通常(如果我使用最大值)至少需要 50 :)
btw, you might want to take a look at this regex tutorial
顺便说一句,你可能想看看这个正则表达式教程
回答by Thomas
preg_match('/^(?=.*\d)(?=.*[@#\-_$%^&+=§!\?])(?=.*[a-z])(?=.*[A-Z])[0-9A-Za-z@#\-_$%^&+=§!\?]{8,20}$/',$password)
- at least one lowercase char
- at least one uppercase char
- at least one digit
- at least one special sign of @#-_$%^&+=§!?
- 至少一个小写字符
- 至少一个大写字符
- 至少一位
- @#-_$%^&+=§! 的至少一个特殊符号?
回答by David Bell
I liked r3bel's answer, so I had a play with it and ended up with the following as a password-checking function:
我喜欢 r3bel 的回答,所以我玩了一下,最后得到了以下作为密码检查功能:
function password_strength_check($password, $min_len = 8, $max_len = 70, $req_digit = 1, $req_lower = 1, $req_upper = 1, $req_symbol = 1) {
// Build regex string depending on requirements for the password
$regex = '/^';
if ($req_digit == 1) { $regex .= '(?=.*\d)'; } // Match at least 1 digit
if ($req_lower == 1) { $regex .= '(?=.*[a-z])'; } // Match at least 1 lowercase letter
if ($req_upper == 1) { $regex .= '(?=.*[A-Z])'; } // Match at least 1 uppercase letter
if ($req_symbol == 1) { $regex .= '(?=.*[^a-zA-Z\d])'; } // Match at least 1 character that is none of the above
$regex .= '.{' . $min_len . ',' . $max_len . '}$/';
if(preg_match($regex, $password)) {
return TRUE;
} else {
return FALSE;
}
}
Max/Min lengths are default or adjustable, each requirement is default on, but can be switched off, and I wanted to support any symbols so the last requirement is "anything that isn't one of the above types", rather than a fixed set of symbols.
最大/最小长度是默认的或可调的,每个要求都是默认的,但可以关闭,我想支持任何符号,所以最后一个要求是“任何不是上述类型之一的东西”,而不是固定的符号集。
回答by Morpheus_ro
I have developed a complete regex for a bit more complex check
我已经开发了一个完整的正则表达式来进行更复杂的检查
/^(?=.*\d)(?=.*[A-Za-z])(?=.*[A-Z])(?=.*[a-z])(?=.*[ !#$%&'\(\) * +,-.\/[\] ^ _`{|}~\"])[0-9A-Za-z !#$%&'\(\) * +,-.\/[\] ^ _`{|}~\"]{8,50}$/
Basically I check for the password to have 1 digit, 1 capital, 1 lower and 1 special character. I hope this helps someone looking for a regex.
基本上我检查密码是否有 1 位数字、1 个大写字母、1 个小写字母和 1 个特殊字符。我希望这可以帮助寻找正则表达式的人。
回答by bhupinder singh
if (!preg_match('/^(?=.*\d)(?=.*[A-Za-z])(?=.*[!@#$%])[0-9A-Za-z!@#$%]
{6,15}$/',($_POST['password']))) {
$message='Password must contain 6 characters of letters, numbers and
at least one special character.';
}
回答by Rishi Kulshreshtha
I've done this with my drupal custom form in hook_form_validatehere the password should be 6 characters of letters, numbers and at least one special character.
我在这里用我的drupal自定义表单完成了这个hook_form_validate密码应该是6个字母、数字和至少一个特殊字符的字符。
<?
if (!preg_match('/^(?=.*\d)(?=.*[A-Za-z])(?=.*[!@#$%])[0-9A-Za-z!@#$%]{6,15}$/', $form_state['values']['pass'])) {
form_set_error('pass', t('Password must contain 6 characters of letters, numbers and at least one special character.'));
}
?>
回答by Amir Fo
A different way
一种不同的方式
Let look different to this, I believe that users can use all of the ASCIIvalid characters (32 < ascii_code < 127)in the password and created a function that checks all the characters that are is in ASCII table!
与此不同,我相信用户可以使用密码中的所有ASCII有效字符(32 < ascii_code < 127),并创建一个函数来检查 ASCII 表中的所有字符!
function check($pass, $i = 0){
if(strlen($pass) > $i)
return (0rd(pass[$i]) > 32 and 0rd(pass[$i]) < 127) and check($pass, $i + 1);
}
Usage
用法
if(!check($_POST["password"])){
echo "password is wrong";
}
回答by Niet the Dark Absol
Look up character classes, a basic feature of regular expressions.
查找字符类,这是正则表达式的一个基本特征。
回答by christine
if(!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{8,12}$/', $password)) {
echo 'the password does not meet the requirements!';
}
In the above statement.. what possible password thus exist?.
在上面的语句中..因此存在什么可能的密码?。

