//validate email
if(empty($email)){
$mailErr = "<div class='errors'>Email can not be empty.</div>";
}else{
if(!(preg_match("/^[a-zA-Z0-9\-_]+(\.[a-zA-Z0-9\-_]+)*@[a-z0-9]+(\-[a-z0-9]+)*(\.[a-z0-9]+(\-[a-z0-9]+)*)*\.[a-z]{2,4}$/", $email))){
$mailErr = "<div class='errors'>Please input a valid email address.</div>";
}
}
//validate message
if (empty($message)) {
$messageErr = "<div class='errors'>Message can not be empty.</div>";
}
如果两个验证均为真,则发送邮件。请帮我。我该怎么写这个条件。如果两个条件都满足,我想发送邮件。
答案 0 :(得分:0)
// Validate email
if (empty($email)) {
$mailErr = "<div class='errors'>Email can not be empty.</div>";
} else if (!(preg_match("/^[a-zA-Z0-9\-_]+(\.[a-zA-Z0-9\-_]+)*@[a-z0-9]+(\-[a-z0-9]+)*(\.[a-z0-9]+(\-[a-z0-9]+)*)*\.[a-z]{2,4}$/", $email))){
$mailErr = "<div class='errors'>Please input a valid email address.</div>";
} else {
$valid_email = true;
}
// Validate message
if (empty($message)) {
$messageErr = "<div class='errors'>Message can not be empty.</div>";
}else{
$valid_message = true;
}
if ($valid_email && $valid_message) {
// Send the email
}
答案 1 :(得分:0)
有一个简短的表单可以使用FILTER_VALIDATE_EMAIL验证电子邮件地址,您可以安全地保存一个包含错误状态的变量。
$error = false;
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$mailErr = "<div class='errors'>Please input a valid email address.</div>";
$error = true;
}
if (empty($message)) {
$messageErr = "<div class='errors'>Message can not be empty.</div>";
$error = true;
}
if(!$error) {
//Send message here
}
您也可以在不使用状态变量的情况下使用简短形式(但不能同时检查两者):
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$mailErr = "<div class='errors'>Please input a valid email address.</div>";
} else if (empty($message)) {
$messageErr = "<div class='errors'>Message can not be empty.</div>";
} else {
//Send message here
}