我在制作带有联系表格的网站时遇到问题。 首先,输入的任何电子邮件地址都是无效的。 此外,我对该名称有所期待,而且我不确定如何为姓氏添加特殊字符(例如É)和空格。
这是现有的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);
} ?&GT;
答案 0 :(得分:0)
我怀疑问题在于你的前任。使用http://gskinner.com/RegExr/对test@test.com进行测试,您的注册表甚至不会获取该基本电子邮件地址。
你不太可能想出一个涵盖所有电子邮件可能性的正则表达式 - 只需检查一个“@”符号,然后将测试邮件发送到服务器以查看它是否有效可能更有价值。
否则,如果您想要有效的正则表达式进行测试,请尝试
/[^\s]*@[a-z0-9.-]*/i