PDA

View Full Version : Regex For Email



cancer10
10-06-2008, 05:28 PM
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";



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

james438
10-11-2008, 12:30 AM
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.


<?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.

cancer10
10-11-2008, 02:28 AM
Thanx mate for your help :)

james438
10-11-2008, 09:21 AM
Doh! I forgot to make the tiny change to allow abc@domain.info to work. Here is what the code should look like:
<?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.