[英]contact form/php case sensitive
我正在使用此联系表格,除了一个例外,它似乎工作正常! 电子邮件地址的输入字段区分大小写。 因此,如果您使用的设备(例如iPad)会自动将电子邮件地址的首字母大写(Name@gmail.com),则该表单将无法识别它是有效的电子邮件地址。 有解决办法吗?
<title>My basic contact form</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="page-header">
<h1>Contact Me</h1>
</div>
<?php
// check for a successful form post
if (isset($_GET['s'])) echo "<div class=\"alert alert-success\">".$_GET['s']."</div>";
// check for a form error
elseif (isset($_GET['e'])) echo "<div class=\"alert alert-error\">".$_GET['e']."</div>";
?>
<form method="POST" action="contact-form-submission.php" class="form-horizontal">
<div class="control-group">
<label class="control-label" for="input1">Name</label>
<div class="controls">
<input type="text" name="contact_name" id="input1" placeholder="Your name">
</div>
</div>
<div class="control-group">
<label class="control-label" for="input2">Email Address</label>
<div class="controls">
<input type="text" name="contact_email" id="input2" placeholder="Your email address">
</div>
</div>
<div class="control-group">
<label class="control-label" for="input3">Message</label>
<div class="controls">
<textarea name="contact_message" id="input3" rows="8" class="span5" placeholder="The message you want to send to me."></textarea>
</div>
</div>
<div class="form-actions">
<input type="hidden" name="save" value="contact">
<button type="submit" class="btn btn-primary">Send</button>
</div>
</form>
</div>
</body>
</html>
//contact-form-submission.php
<?php
// check for form submission - if it doesn't exist then send back to contact form
if (!isset($_POST["save"]) || $_POST["save"] != "contact") {
header("Location: contact-form.php"); exit;
}
// get the posted data
$name = $_POST["contact_name"];
$email_address = $_POST["contact_email"];
$message = $_POST["contact_message"];
// check that a name was entered
if (empty ($name))
$error = "You must enter your name.";
// check that an email address was entered
elseif (empty ($email_address))
$error = "You must enter your email address.";
// check for a valid email address
elseif (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $email_address))
$error = "You must enter a valid email address.";
// check that a message was entered
elseif (empty ($message))
$error = "You must enter a message.";
// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
header("Location: contact-form.php?e=".urlencode($error)); exit;
}
// write the email content
$email_content = "Name: $name\n";
$email_content .= "Email Address: $email_address\n";
$email_content .= "Message:\n\n$message";
// send the email
mail ("somename@gmail.com", "New Contact Message", $email_content);
// send the user back to the form
header("Location: contact-form.php?s=".urlencode("Thank you for your message.")); exit;
?>
正如我在评论中所说,您可以将strtolower()
与当前方法一起使用。
更改
$email_address = $_POST["contact_email"];
至
$email_address = strtolower($_POST["contact_email"]);
并将输入的所有内容转换为小写。
但是,建议您使用FILTER_VALIDATE_EMAIL验证电子邮件。
例:
$email_address = strtolower($_POST["contact_email"]);
if(!filter_var($email_address, FILTER_VALIDATE_EMAIL))
{
echo "E-mail is not valid"; exit;
}
elseif (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $email_address))
至
elseif (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email_address))
特别是,您需要preg_match上的“ i”标志。 有关更多信息,请参见此答案
顺便说一句,这是一个真正的丑陋的正则表达式..大声笑...这是我使用的。
/^[^@]+@[-\w._]+\.[a-zA-Z]+$/
演示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.