+ Reply to Thread
Results 1 to 10 of 10

Thread: Email Validation

  1. #1
    phpforfun's Avatar
    phpforfun is offline Speaks fluent binary
    Join Date
    Feb 2008
    Posts
    1,232
    Blog Entries
    17
    Rep Power
    24

    Email Validation

    ok first off let me say.. This NOT my code, I messed with it a bit, but I honestly forgot where I got it, I just googled it from somewhere, but it works GREAT. use it to check if an email is in the right format.
    Function:
    Code:
    function check_email_address($email) {

    if (!
    ereg("^[^@]{1,64}@[^@]{1,255}$"$email)) {

    return 
    false;

    }



    $email_array explode("@"$email);

    $local_array explode("."$email_array[0]);

    for (
    $i 0$i sizeof($local_array); $i++) {

    if (!
    ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$"$local_array[$i])) {

    return 
    false;

    }

    }

    if (!
    ereg("^\[?[0-9\.]+\]?$"$email_array[1])) {

    $domain_array explode("."$email_array[1]);

    if (
    sizeof($domain_array) < 2) {

    return 
    false

    }

    for (
    $i 0$i sizeof($domain_array); $i++) {

    if (!
    ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$"$domain_array[$i])) {

    return 
    false;

    }

    }

    }

    return 
    true;


    Use like this:
    Code:
    if(!check_email_address($email)) {
           
    //code to use for incorrect email
    } else {
           
    //code to use for correct email

    Again.. not my code, but I thought you could use it!
    Checkout my new forum! http://adminreference.com/

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Whitey's Avatar
    Whitey is offline Programming Professional
    Join Date
    Feb 2008
    Location
    Loveland, Colorado
    Posts
    254
    Rep Power
    18

    Re: Email Validation

    Nice thanks.. I will actually use this.. I was going to try and make something like this for my register script.. but i gave up on it.. So this can come in handy

  4. #3
    phpforfun's Avatar
    phpforfun is offline Speaks fluent binary
    Join Date
    Feb 2008
    Posts
    1,232
    Blog Entries
    17
    Rep Power
    24

    Re: Email Validation

    its awesome dude, its a great script, havent had a problem with it yet
    Checkout my new forum! http://adminreference.com/

  5. #4
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Email Validation

    There are two alternatives here: PHP: Validate Email Address
    One from Jordan and one from myself. The more the merrier

  6. #5
    phpforfun's Avatar
    phpforfun is offline Speaks fluent binary
    Join Date
    Feb 2008
    Posts
    1,232
    Blog Entries
    17
    Rep Power
    24

    Re: Email Validation

    very nice sir!
    Checkout my new forum! http://adminreference.com/

  7. #6
    ReekenX's Avatar
    ReekenX is offline Programmer
    Join Date
    Jan 2007
    Location
    Lithuania
    Posts
    135
    Rep Power
    0

    Re: Email Validation

    Can any body tell me right e-mail format. In phpforfun code, I see {1,64} before @ and {1,255} after @. So in databases e-mail column must be size of 64+1+255?

  8. #7
    acccapulco is offline Newbie
    Join Date
    Aug 2008
    Posts
    5
    Rep Power
    0

    Re: Email Validation

    I am using this function (and think its ok)
    Code:
    function test_email($email) {
        
    $match "/"
            
    "^[a-z0-9_-]+"
            
    "(\.[a-z0-9_-]+)*"
            
    "@"
            
    "[a-z0-9][a-z0-9-]*"
            
    "(\.[a-z0-9-]+)*"
            
    "\.([a-z]{2,6})$"
            
    "/i";
        return 
    preg_match($match$email) ? true false;


  9. #8
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Email Validation

    Quote Originally Posted by ReekenX View Post
    Can any body tell me right e-mail format. In phpforfun code, I see {1,64} before @ and {1,255} after @. So in databases e-mail column must be size of 64+1+255?
    Correct. According to the RFC a valid email address contains a local-part@domain. As defined by the RFC, the local-part must be a maximum of 64 characters. The domain, defined in another section of the RFC says that a domain may contain labels between 1 and 63 characters inclusive. When all labels and dots are combined, it may not exceed 255 characters. Which suggests an email address can be of the form a@b.c.d.e

    That said, no TLD that I know of (com, net, org) exceeds 6 characters (.museum is the largest I know). So allowing the TLD would be poor validation in my book.

  10. #9
    DiscoStu is offline Newbie
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Re: Email Validation

    I've been looking for something like this, so thanks.

    One question though, my understanding is that when ICANN allows custom TLDs we're going to see email addresses such as drink@coke (with NO .com on the end)

    Am I right in assuming that this will break pretty much every email validation script currently in use?

  11. #10
    SoN9ne's Avatar
    SoN9ne is offline Programmer
    Join Date
    Mar 2010
    Location
    Juno Beach, Florida, United States
    Posts
    125
    Rep Power
    0

    Re: Email Validation

    It is better to use PHP built in filters for this...
    Not only can they process this much faster, they have been built off of experience and standards, not to mention they have been tested by many programmers.
    To build your own is not only a waste of code and time... you are re-inventing the wheel.

    Here's an example of the email filter.
    Code:
    if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    	echo 'passed';
    } else {
    	echo 'failed';
    }
    I would recommend that you learn these filters, they can save you a lot of time coding.
    Of course, this info is always available in the PHP: filter_var - Manual

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Validation help
    By movax85 in forum General Programming
    Replies: 7
    Last Post: 03-23-2011, 05:49 AM
  2. Reading email from external email account
    By amrosama in forum PHP Development
    Replies: 5
    Last Post: 01-25-2010, 07:23 PM
  3. Easy Email Validation
    By kresh7 in forum Visual Basic Tutorials
    Replies: 5
    Last Post: 01-19-2009, 08:07 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts