Re: Ereg() Function
| From: | Ernest E Vogelsinger | Date: | Fri, 23 May 2003 21:36:25 +0000 |
| Subject: | Re: Ereg() Function | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-148816@lists.php.net to get a copy of this message | ||
At 23:18 23.05.2003, Daniel J. Rychlik said:
--------------------[snip]--------------------
>If I wanted to check for a valid email address, basically and email that has
>@ and some domain like .edu, .net or something, why wont this work ?
>
>if (ereg ("^[\w.-]+@[\w.-]+\.(com|net|org|edu|mil|gov|gob|info|tv|biz)
>+(\.[A-Za-z]{2}|\s*)$", $_POST["emailaddy"])) {
>
> echo "doesnt appear to be a valid mail addy";
>}
>Im not understanding why $_POST["emailaddy"] is not being evaluated
>correctly. Im putting in non-valid email addresses and I expect this to
>return true, but it doesnt. Any Ideas.?
>-Dan
--------------------[snip]--------------------
I believe it's the '.' character in your character sets above - the dot
represents "any character". You need to escape this with a backslash:
if (ereg
("^[\w\.-]+@[\w\.-]+\.(com|net|org|edu|mil|gov|gob|info|tv|biz)+(\.[A-Za-z]{
2}|\s*)$", $_POST["emailaddy"])) {
Note that your expression will rule out _all_ national top level domains,
like "us", "ca", "br", etc... there are a lot of these, and you might
want
to check for a valid TLD in an extra step, if at all.
I'm using preg_match to validate syntactically correct email addresses
using this expression:
if (!preg_match('/([a-z0-9\-\.\#\:]+@[a-z0-9\-]+\.[a-z0-9\-\.]+)/i',
$value, $aresult)) {
I do not know if that allows all RFC compliant addresses (including routing
information that _may_ be part of the address), but didn't have any
complaint yet, and have _no_ invalid email addresses in our databases so far.
BTW, the preg functions are said to be faster than ereg.
HTH,
--
>O Ernest E. Vogelsinger
(\) ICQ #13394035
^ http://www.vogelsinger.at/