这是我的代码:
class FormValidator {
private $firstname;
private $lastname;
private $email;
private $fields_with_lengths = array('first' => 2, 'last' => 2);
private $color = '<span style="color:#FF0000">';
function checkFirst($firstname) {
$first_error = NULL;
if(strlen(trim($firstname)) < $this->fields_with_lengths['first']){
$first_error = $this->color . 'Please enter more than ' . $this->fields_with_lengths['first'] . ' characters.</span>';
}
return $first_error;
}
function checkLast($lastname) {
$last_error = NULL;
if(strlen(trim($lastname)) < $this->fields_with_lengths['last']){
$last_error = $this->color . 'Please enter more than ' . $this->fields_with_lengths['last'] . ' characters.</span>';
}
return $last_error;
}
function validateEmail($email){
return preg_match('/^[^@]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$/', $email);
}
}
这就是我所说的:
$validator = new FormValidator();
$firstResult = $validator->checkFirst($_POST['firstname']);
$lastResult = $validator->checkLast($_POST['lastname']);
$emailResult = $validator->validateEmail($_POST['emailaddress1']);
if (is_null($firstResult) && is_null($lastResult) && $emailResult) {
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->AddAddress("s...@....com");
$mail->Subject = "test";
$mail->MsgHTML($messageHTML);
redirectULS('english/forms/thankyou.php');
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
//$bridge->pushLead($lead);
}
}
仅当我从if语句中删除$ emailResult时才有效。我究竟做错了什么。我将考虑之前关于仅在以后返回true或false的函数的注释。现在我需要修复这个if语句。谢谢。 (有些变量在别处设置。我想知道的是如何使if语句起作用。)
答案 0 :(得分:1)
var_dump($firstResult, $lastResult, $emailResult);
preg_match()返回的数量 时代模式匹配。那将是 0次(不匹配)或1次 因为preg_match()会停止 在第一场比赛后搜索。 preg_match_all()恰恰相反 继续,直到它结束 学科。如果,preg_match()返回FALSE 发生错误。
$email = "me@me.com";
echo preg_match('/^[^@]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$/', $email); //1 (true)
$email = "me+me.com";
echo preg_match('/^[^@]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$/', $email); //0 (this is not false)
$email = "me@me.com";
if(1 == 1 && 0 == 0 && preg_match('/^[^@]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$/', $email)){
echo "OK"; //yup
}
答案 1 :(得分:0)
preg_match返回给定模式的匹配数(1或0)。你在这里以非常奇怪的方式进行评估。你可能想检查一下。
答案 2 :(得分:0)
老兄,我做了一个测试,你的代码运作正常。 你能把错误信息放在这里吗?