Page 1 of 1

eregi help please

Posted: Sun Feb 02, 2003 9:56 am
by lostboy
i have the following code trying to validate an email address...

Code: Select all

if ((strlen($email)>=5)&&(strlen($email)<50))&#123;   //min length 5 and max length 50
        if ((!eregi("^&#1111;www]&#123;3&#125;",$email))||(!eregi("\@setroPETS\.com$",$email)))&#123;
        //the above denies emails starts with 'www' or denies emails with endings 'setroPETS.com'
        //to prevent useless emails from being entered
             if (!eregi("(&#1111;A-Za-z0-9])&#123;4,15&#125;$", $email))&#123;    //prevent emails with 'aaaaaa' or '888888'

                if (eregi("^&#1111;a-zA-Z0-9\.\_]+@&#1111;a-zA-Z0-9]+\.&#1111;a-zA-Z0-9\.]&#123;2,4&#125;$", $email))&#123;
The problem is in the line that is trying to check for emails starting with 'www'. I am trying to prevent emails with www in the username and ones with the domain name 'setropets.com' to prevent useless email addresses from clodding the db?

Currently it does allow something like http://www.bob@bob.com and bob@setropets.com

any ideas as to what i got wrong?

Posted: Sun Feb 02, 2003 1:32 pm
by Stoker
I think you are missunderstanding the posix-regex syntax a bit, ([A-z]){2,5} doesn't match 2 to 5 equal characters.. Anyway, I would highly recommend not using posix (ereg), instead use perl regex, it is a lot more efficient.. and When looking for specific text (like www), use str-functions instead.

A full regex for validating all possibilities of an email address is huge, but one that should work for 99% could be something like (untested)

if (preg_match(
'/^[A-z0-9][A-z0-9\\-._]+@[A-z0-9][A-z0-9\\-]+(\\.[A-z0-9][A-z0-9\\-]+)*?\\.[A-z][A-z]+$/',$email)) {
# email address is ok..
}

to find any occurence of 'www'
if (stristr($email,'www')) { /* found www */ }
use the same for matching other strings like domain names etc

what method is most efficient for finding repeated chars I don't know really, perhaps someone else has a good idea on that?

maybe something like
preg_match('([A-z])\\1{3,100}')
i think would match 4 to 100 equeal characters..

Posted: Sun Feb 02, 2003 1:35 pm
by daven
For general email formatting: the email functions at killersoft (http://www.killersoft.com/downloads/paf ... egory&id=1) are great. They check for legal email names/formats. I highly suggest using them.