note 79991 deleted from function.preg-match by felipe
| From: | felipe@php.net | Date: | Sun, 23 Mar 2008 22:56:16 +0000 |
| Subject: | note 79991 deleted from function.preg-match by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-137378@lists.php.net to get a copy of this message | ||
Note Submitter: BK
----
I found Frebby's post below from 28-Oct-2007 to be a rock solid way to validate a user's
e-mail address, and it even accepts subdomains such as .co.uk. However, it fails if the user or the
browser adds a space before the text entry (this sometimes happens when clicking into a form field,
particularly in IE 6).
There may be other ways to address that problem, but here's a simple fix that works well within
basic form processing scripts:
<?php
$email = $_POST["email"];
$errorurl = "your error page URL here" ;
if (!preg_match("/^[\ a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,6}$/i", $email)) {
header( "Location: $errorurl" );
exit ;
}
?>
The difference? Just add a backwards slash and a space before the a-z portion of the first segment:
[\ a-z0-9._-]+@
That's it! Enjoy