1

我在制作带有联系表格的网站时遇到问题。首先,输入的任何电子邮件地址都会显示为无效。另外,我对名字有期望,但我不确定如何为姓氏添加特殊字符(例如 É)和空格。

这是现有的PHP:

$error_message = "";
$email_exp = "/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z];{2,4}$/";

if (!preg_match($email_exp, $email)) {
    $error_message .= '<b> The Email Address you entered does not appear to be valid. </b> <br/>';
}

$string_exp = "/^[A-Za-z.'-]+$/";   
if (!preg_match($string_exp, $name)) {
    $error_message .= '<b> The Name you entered does not appear to be valid. </b> <br/>';
}

if(strlen($subject) < 3){
    $error_message .= '<b> The Subject you entered does not appear to be valid. </b> <br/>';    
}

if(strlen($message) < 2){
    $error_message .= '<b> The Message you entered does not appear to be valid. </b> <br/>';    
}

更新了 @J 的 PHP。罗伯逊:

<?php

if (isset($_POST['email'])){

// email info
$email_to ="example@example.com";
$email_subject = $_POST['subject'];
$email_from = $_POST['email'];

//error code

function died($error) {
    echo "I am sorry, but there seems to be error(s) found within the form you submitted. <br/>";
    echo "The error(s) will appear below: <br/> <br/>";
    echo $error. "<br/><br/>";
    echo "Please go back and fix these error(s). <br/>";
    die();
    }

//validation

    if(!isset($_POST['name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['subject']) || 
    !isset($_POST['message'])) {
        died('I am sorry, but there appears to be a problem with the form you submitted.');
        }

    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

//expected strings

$error_message = "";
$email_exp = '/[^\s]*@[a-z0-9.-]*/i';

if (!preg_match($email_exp, $email)) {
    $error_message .= '<b> The Email Address you entered does not appear to be valid. </b> <br/>';
    }

$string_exp = "/^[A-Za-z.'-]+$/";   
if (!preg_match($string_exp, $name)) {
    $error_message .= '<b> The Name you entered does not appear to be valid. </b> <br/>';
    }
    if(strlen($subject) < 3){
    $error_message .= '<b> The Subject you entered does not appear to be valid. </b> <br/>';    
    }

if(strlen($message) < 2){
    $error_message .= '<b> The Message you entered does not appear to be valid. </b> <br/>';    
    }

if(strlen($error_message) > 0 ){
    died($error_message);
    }
    $email_message = "Form Details below.\n\n";

//sanitization
function clean_string($string){ 
    $bad = array("content-type", "bcc:", "to:", "cc:", "href");
    return str_replace($bad, "", $string);
    }

$email_message .= "Name:" . clean_string($name) . "\n";
$email_message .= "E-Mail:" . clean_string($email) . "\n";
$email_message .= "Subject:" . clean_string($subject) . "\n";
$email_message .= "Message:" . clean_string($message) . "\n";

//email headers
$headers = 'From: ' .$email_From . '\r\n'. 'Reply-To' .
    $email. "\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);

} ?>

4

1 回答 1

0

我怀疑问题出在你的注册前。使用http://gskinner.com/RegExr/对照 test@test.com 对其进行测试,您的 reg ex 甚至不会接收到那个基本的电子邮件地址。

您不太可能想出一个涵盖所有电子邮件可能性的正则表达式 - 简单地检查“@”符号可能更值得,然后向服务器发送一封测试电子邮件以查看它是否有效。

否则,如果您想要一个有效的正则表达式进行测试,请尝试

/[^\s]*@[a-z0-9.-]*/i
于 2013-08-06T18:36:58.027 回答