Regex For Email

Discussion in 'PHP' started by cancer10, Oct 6, 2008.

  1. #1
    Hi PPL,

    I wrote a regex to verify an email address

    
    if(!preg_match("^[a-z0-9]+@{1}[a-z0-9-]+\.{1}[a-z]{2,4}\.?[a-z]{0,2}$",$myemail))
    echo "Invalid Email";
    
    Code (markup):

    Please take a look and tell me if this will work for all type of emails, For example:


    OR

    OR


    and other emails also...


    Thanx
     
    cancer10, Oct 6, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    Why using regex?

    If you are running PHP 5.2.1 or higher you can use FILTER_VAR with FILTER_VALIDATE_EMAIL, just as easy as it can be ;)
     
    EricBruggema, Oct 6, 2008 IP
  3. lui2603

    lui2603 Peon

    Messages:
    729
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #3
    just do if(!filter_var('bla@bla.bla', FILTER_VALIDATE_EMAIL))...
     
    lui2603, Oct 6, 2008 IP
  4. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    What is if I use regex?

    more resource consumption? more time taken or what?
     
    cancer10, Oct 6, 2008 IP
  5. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Why use something else to do something if it is already built in?:) It won't hurt anything, but why bother? Just follow the given code.
     
    NatalicWolf, Oct 6, 2008 IP