我想检查用户是否已成功满足以下要求:
我该怎么做?
我正在使用下面的PHP脚本:
if ( strlen( $password ) < 8 ) {
false
} else {
if ( preg_match( "/[^0,9]/", $password ) ) {
// how to check the upper case and lower case
}
}
答案 0 :(得分:9)
你可以用正则表达式来做到这一点:
if (!preg_match('/^(?=[a-z])(?=[A-Z])[a-zA-Z]{8,}$/', $password))
{
//error
}
答案 1 :(得分:3)
使用preg_match("/[A-Z]/")
和preg_match("/[a-z]/")
答案 2 :(得分:2)
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
}
答案 3 :(得分:2)
您可以使用密码排名技术:
$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>";
答案 4 :(得分:1)
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. */
}
答案 5 :(得分:1)
您可以使用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. */
}
答案 6 :(得分:0)
preg_match('/[a-z]/', $password) && preg_match('/[A-A]/', $password)
答案 7 :(得分:0)
在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.';
}
我给你的剧本;检查长度、复杂性(包含数字、大写、小写以及是否需要特殊字符)