我想检查用户是否成功满足以下要求:
- 密码至少有8个字符
- 由一个资本&一个小写字母
我该怎么做?
我正在使用以下PHP脚本:
if ( strlen( $password ) < 8 ) {
false
} else {
if ( preg_match( "/[^0,9]/", $password ) ) {
// how to check the upper case and lower case
}
}
您可以使用正则表达式:
if (!preg_match('/^(?=[a-z])(?=[A-Z])[a-zA-Z]{8,}$/', $password))
{
//error
}
使用preg_match("/[A-Z]/")
和preg_match("/[a-z]/")
if( strlen($password) < 8 ) {
return false;
}
if(preg_match("/[^0,9]/", $password)) {
how to check the upper case and lower case
}
if($password == strtoupper($password) || $password == strtolower($password)){
//pass fails because its either all upcase, or lowercase
}
您可以使用密码排名技术:
$x = "a12ASD!@#$";
$rank = Array();
$rank['length'] = strlen($x);
$matches = Array();
preg_match_all("/([a-z]+)/", $x, $matches);
$rank['lowercase'] = strlen(implode('', $matches[0]))/count($matches[0]);
$matches = Array();
preg_match_all("/([A-Z]+)/", $x, $matches);
$rank['uppercase'] = strlen(implode('', $matches[0]))/count($matches[0]);
$matches = Array();
preg_match_all("/([0-9]+)/", $x, $matches);
$rank['numbers'] = strlen(implode('', $matches[0]))/count($matches[0]);
$matches = Array();
preg_match_all("/([^a-zA-Z0-9]+)/", $x, $matches);
$rank['symbols'] = strlen(implode('', $matches[0]))/count($matches[0]);
echo "<pre>";
var_dump($rank);
echo "</pre>";
if (
strlen($password) >= 8) &&
preg_match('/[A-Z]/', $password) > 0 &&
preg_match('/[a-z]/', $password) > 0 )
{
/* Password validation passes, do stuff. */
}
else {
/* Password validation fails, show error. */
}
您可以使用trim,它实际上比regexp 快得多
if ( trim( $password, 'a..z') != '' && trim( $password, 'A..Z') != '' && strlen($password) >= 8 )
{
/* Password validation passes, do stuff. */
}
else {
/* Password validation fails, show error. */
}
要验证用户是否满足php端的密码要求,如下所示。
// Given password
$password = 'user-input-pass';
// Validate password strength
$uppercase = preg_match('@[A-Z]@', $password);
$lowercase = preg_match('@[a-z]@', $password);
$number = preg_match('@[0-9]@', $password);
$specialChars = preg_match('@[^w]@', $password);
if(!$uppercase || !$lowercase || !$number || !$specialChars || mb_strlen($password) < 8) {
echo 'Password should be at least 8 characters in length and should include at least one upper case letter, one number, and one special character.';
}else{
echo 'Strong password.';
}
我给你的剧本;检查长度、复杂性(包含数字、大写、小写,如果需要特殊字符)
preg_match('/[a-z]/', $password) && preg_match('/[A-A]/', $password)
此函数允许您通过使用preg_replace:中的count参数计数出现次数来设置最低要求
function password_validate($password, $min_length=8, $min_lowercases=1, $min_uppercases=1, $min_numbers=1, $min_specials=0) {
preg_replace('#[a-z]#', '', $password, -1, $lowercases);
preg_replace('#[A-Z]#', '', $password, -1, $uppercases);
preg_replace('#[0-9]#', '', $password, -1, $numbers);
preg_replace('#[^w]#', '', $password, -1, $specials);
return (mb_strlen($password) >= $min_length && $lowercases >= $min_lowercases && $uppercases >= $min_uppercases && $numbers >= $min_numbers && $specials >= $min_specials);
}
它可以这样使用:
if (!password_validate($new_password)) {
echo 'Password did not meet requirements';
}
或通过要求:
if (!password_validate($new_password, 6, 1, 0, 0, 0)) {
echo 'Password did not meet requirements';
}