note 79991 added to function.preg-match

From: Date: Sat, 22 Dec 2007 20:20:28 +0000
Subject: note 79991 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-133886@lists.php.net to get a copy of this message
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 ---- Server IP: 64.71.164.2 Probable Submitter: 168.253.15.58 ---- Manual Page -- http://www.php.net/manual/en/function.preg-match.php Edit -- https://master.php.net/note/edit/79991 Del: integrated -- https://master.php.net/note/delete/79991/integrated Del: useless -- https://master.php.net/note/delete/79991/useless Del: bad code -- https://master.php.net/note/delete/79991/bad+code Del: spam -- https://master.php.net/note/delete/79991/spam Del: non-english -- https://master.php.net/note/delete/79991/non-english Del: in docs -- https://master.php.net/note/delete/79991/in+docs Del: other reasons-- https://master.php.net/note/delete/79991 Reject -- https://master.php.net/note/reject/79991 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#133886) next »