联系表格/ php区分大小写

时间:2014-08-23 00:17:23

标签: php html forms email post

我正在使用这个联系表单似乎工作正常,但有一个例外! 电子邮件地址的输入字段区分大小写。 因此,如果你使用像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;

?>

2 个答案:

答案 0 :(得分:1)

正如我在评论中所说,您可以使用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;
  }

答案 1 :(得分:0)

 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'标志。有关更多信息,请参阅此答案

how do I make this preg_match case insensitive?

顺便说一下,这是一个真正丑陋的regx ..大声笑......这是我使用的那个。

 /^[^@]+@[-\w._]+\.[a-zA-Z]+$/

演示

http://regex101.com/r/zH1iJ2/1