Reputation: 1
I'm using this contact form which seems to work fine with one exception! The input field for the email address is case sensitive. So if you're using a device like an iPad, which automatically capitalizes the first letter of the email address ([email protected]) The form will not recognize it is a valid email address. Is there a fix for this?
<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 ("[email protected]", "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;
?>
Upvotes: 0
Views: 1965
Reputation: 74217
As I said in my comment, you can use strtolower()
for this, along with your present method.
Change
$email_address = $_POST["contact_email"];
to
$email_address = strtolower($_POST["contact_email"]);
and it will transform everything entered to lowercase.
However, I suggest you use FILTER_VALIDATE_EMAIL to validate Email with.
Example:
$email_address = strtolower($_POST["contact_email"]);
if(!filter_var($email_address, FILTER_VALIDATE_EMAIL))
{
echo "E-mail is not valid"; exit;
}
Upvotes: 1
Reputation: 21661
elseif (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $email_address))
To
elseif (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email_address))
Specially, you want the 'i' flag on the preg_match. For more information see this answer
how do I make this preg_match case insensitive?
By the way that is a real ugly regx .. lol ... This is the one I use.
/^[^@]+@[-\w._]+\.[a-zA-Z]+$/
Demo
http://regex101.com/r/zH1iJ2/1
Upvotes: 0