Results 1 to 4 of 4

Thread: Regex For Email

  1. #1
    Join Date
    Dec 2007
    Posts
    123
    Thanks
    17
    Thanked 1 Time in 1 Post

    Question Regex For Email

    Hi PPL,

    I wrote a regex to verify an email address

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

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

    abc@domain.co.uk
    OR
    abc@domain.info
    OR
    abc@domain.com

    and other emails also...


    Thanx

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    I can't seem to get your code to work for abc@domain.info. I want to add that I do not know much about regex or PCRE, but I went to http://regexadvice.com/forums/thread/46502.aspx and found the following code that seems to work for all emails.

    Code:
    <?php
    $test='abc@domain.info';
    if(preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', 
            $test)) {echo "YES";}
    else {echo "NO";}
    ?>
    I altered it just slightly to recognize abc@domain.info. Let me know if you find any email address that does not work for that code.

  3. #3
    Join Date
    Dec 2007
    Posts
    123
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default

    Thanx mate for your help

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Doh! I forgot to make the tiny change to allow abc@domain.info to work. Here is what the code should look like:
    Code:
    <?php
    $test='abc@domain.info';
    if(preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/', 
            $test)) {echo "YES";}
    else {echo "NO";}
    ?>
    please disregard my earlier post.

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
  •