我正在尝试使用preg_match和RegEx验证密码,但它似乎不起作用。我想要做的是:确保密码符合以下最低条件: - 包含混合大小写字母 - 包含至少一个号码 - 只要符合上述两个条件,其余部分就可以是任何东西。
我尝试了以下RegEx,但它似乎无法正常工作:
(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])
我之前还有其他更容易的RegEx'es:[A-Za-z0-9]
,但没有成功。我正在检查preg_match($string, $pattern) == 0
(意味着模式不匹配=>验证失败)但它总是返回0.我做错了什么?
答案 0 :(得分:3)
只需在你的正则表达式中添加一个起始锚点,
^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])
OR
^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9]).*
示例:
$yourstring = 'Ab';
$regex = '~^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])~m';
if (preg_match($regex, $yourstring)) {
echo 'Yes! It matches!';
}
else {
echo 'No, it fails';
} // No, it fails
答案 1 :(得分:3)
我总是尽量避免使用正则表达式,所以我对问题采取了不同的方法。以下代码将测试至少一个大写,一个小写和一个数字的密码。
function isValidPassword($password)
{
$hasUppercase = false;
$hasLowercase = false;
$hasDigit = false;
foreach (str_split($password) as $char)
{
$charAsciiValue = ord($char);
if ($charAsciiValue >= ord('A') && $charAsciiValue <= ord('Z')) {
$hasUppercase = true;
}
if ($charAsciiValue >= ord('a') && $charAsciiValue <= ord('z')) {
$hasLowercase = true;
}
if ($charAsciiValue >= ord('0') && $charAsciiValue <= ord('9')) {
$hasDigit = true;
}
}
return $hasUppercase && $hasLowercase && $hasDigit;
}
var_dump(isValidPassword('Ab9c'));
var_dump(isValidPassword('abc'));
bool(true)
bool(false)
答案 2 :(得分:1)
我提供了一个不同的解决方案,主要是因为regexp几乎没有提供错误报告,因此你必须手动测试字符串,以获得凝聚力。考虑将模式分开并添加自己的错误。迭代每个要求,并测试模式。将错误推入数组并检查其存在。将预先声明的变量返回为true / false,以便使用if(validate_password($pass)):
进行验证。这是模型:
function validate_password($pass){
$requirements = array();
//uppercase
$requirements['uppercase']['pattern'] = '/[A-Z]/';
$requirements['uppercase']['error'] = 'Your password must contain at least one uppercase letter.';
//lowercase
$requirements['lowercase']['pattern'] = '/[a-z]/';
$requirements['lowercase']['error'] = 'Your password must contain at least one lowercase letter.';
//requires a number
$requirements['number']['pattern'] = '/[0-9]/';
$requirements['number']['error'] = 'Your password must contain at least one number.';
//special characters
$requirements['special_character']['pattern'] = '/[!@#$%^&*()\\-_=+{};\:,<\.>]/';
$requirements['special_character']['error'] = 'Your password must contain at least one special character.';
//length
$requirements['length']['pattern'] = '/^.{8,}/';
$requirements['length']['error'] = 'Your password must be at least 8 characters in length total.';
$is_valid = false; //our flag to return as true once all tests have passed.
$errors = false;
//validate all requirements
foreach($requirements as $idx => $req):
if(preg_match($req['pattern'], $pass, $matches)):
$is_valid = true;
else:
$errors[] = $req['error'];
$is_valid = false;
endif;
endforeach;
//if we had errors above
if($errors):
$is_valid = false;
foreach($errors as $error):
echo '<p>', $error, '</p>';
endforeach;
endif;
return $is_valid;
}
$pass = 'j!Adz6'; //change this to test
echo validate_password($pass);
一个eval.in例子让你高兴。