Validate Email address with PHP

Discussion in 'PHP' started by webmyne, Feb 15, 2010.

  1. #1
    I have my website completely made in PHP. But I m stuck in the Email address validation. How can i do that with PHP

    More Info
    webmyne.com
     
    webmyne, Feb 15, 2010 IP
  2. bugcoder

    bugcoder Peon

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2

    <?php
    /**
    * Most corrected pattern for Email validation.
    *
    */

    // Valid email
    echo preg_match('/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*
    \@([a-z0-9])*(\.([a-z0-9])([-a-z0-9_-])([a-z0-9])+)*$/i'
    ,'09_az..AZ@host.dOMain.cOM');

    // Invalid emails
    echo preg_match('/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*
    \@([a-z0-9])*(\.([a-z0-9])([-a-z0-9_-])([a-z0-9])+)*$/i'
    ,'09_azAZ@ho...st...........domain.com');

    echo preg_match('/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*
    \@([a-z0-9])*(\.([a-z0-9])([-a-z0-9_-])([a-z0-9])+)*$/i'
    ,'09_azAZ@host.do@main.com');
    ?>
    ----------------------------
    Output:
    ----------------------------
    1 = valid
    0 = invalid
    0 = invalid

    for more details

    http://php.net/manual/en/function.preg-match.php
     
    bugcoder, Feb 15, 2010 IP
  3. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #3
    Regular expressions, to validate the email format.
     
    danx10, Feb 15, 2010 IP
  4. Will Bontrager

    Will Bontrager Peon

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Developing a regex to match all valid email addresses or
    fail to match all invalid addresses may be impossible. Most
    can be matched, but probably not all. See a short list of
    URLs at the end of this note for more email address regex
    information.

    If most is good enough, then a complex or even a simple
    regular expression may be an acceptable compromise.

    The regular expression on this page may be the most complex
    email address validation regex ever devised. The regular
    expression contains 6343 characters.

    http://ex-parrot.com/~pdw/Mail-RFC822-Address.html

    Unless your project requires very strict checking, checking
    for invalid formats may be sufficient. Check for things like
    a missing @ or more than one @; a comma or semi-colon; and
    consecutive dots on the domain side of the @.

    Rejecting unless the address matches a certain format is
    likely to sooner or later reject a deliverable email
    address.

    As an aside, but related: The PHP preg_... functions do text
    processing with Perl-compatible regular expressions. The
    Internet is rich with juicy examples of tried and tested
    regular expression that can be ported from Perl to PHP with
    nary a change in the expression itself. It can be a fruitful
    avenue of research for sticky regex problems.

    URLs to some email address regex information:

    http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/ckaddr.gz
    (Download. A great example of email address validation.)

    http://docstore.mik.ua/orelly/linux/cgi/ch09_02.htm
    http://docstore.mik.ua/orelly/perl/cookbook/ch06_20.htm
    http://faq.perl.org/perlfaq9.html#How_do_I_check_a_val

    Will
     
    Will Bontrager, Feb 15, 2010 IP
  5. Will Bontrager

    Will Bontrager Peon

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    My apologies for the lack of live links in the previous post. It seems the system doesn't trust me enough to publish links. Maybe you shouldn't trust me, either :eek:

    Will
     
    Will Bontrager, Feb 15, 2010 IP
  6. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    krsix, Feb 15, 2010 IP