如果我把你的验证规则的功能,有可能测试不同的输入,并查看它们是否按预期工作。通过一些值
<?php
function reset_password_validator($password, $repeat_password) {
if(empty($password) && empty($repeat_password)){
echo "Please fill in both fields!";
}
else{
if(empty($password) || empty($repeat_password)){
if(empty($password)){
echo "Please fill in 'New Password' field!";
}
if(empty($repeat_password)){
echo "Please fill in 'Confirm Password' field!";
}
}
else {
if ($password != $repeat_password){
echo "Passwords aren't matching!";
}
else {
if(strlen($password) <= 7) {
echo "Password must be minimum 8 characters long!";
}
}
}
}
}
执行命令
$inputs = array(
array('',''),
array('foo', ''),
array('foo', 'bar'),
array('', 'bar'),
array('foo', 'foo'),
array('password', 'password')
);
foreach($inputs as $pair)
{
printf("Validating: '%s' and '%s'\n", $pair[0], $pair[1]);
reset_password_validator($pair[0], $pair[1]);
print "\n";
}
输出:
Validating: '' and ''
Please fill in both fields!
Validating: 'foo' and ''
Please fill in 'Confirm Password' field!
Validating: 'foo' and 'bar'
Passwords aren't matching!
Validating: '' and 'bar'
Please fill in 'New Password' field!
Validating: 'foo' and 'foo'
Password must be minimum 8 characters long!
Validating: 'password' and 'password'
我被诱惑与一般错误,以简化,并删除长度要求:
if(empty($password) || $password !== $repeat_password) {
$error = 'Password fields must match and not be empty.';
}
当你说它不工作,什么究竟是不是按预期工作? – Terry
@Terry我的意思是它不验证它。如果我们谈论流量,它甚至不会在第一块中获得。当我把两个字段都清空时,它会通过验证 –
该脚本没有任何问题。我试了一下,并从$ rstPwd1 = $ _POST ['rstPwd1'];到$ rstPwd1 =“”;对于$ rstPwd1也是如此,它验证失败。这与你如何提交数据最相似 –