Regex for email validation

This is a discussion on Regex for email validation within the PHP forums in Programming Languages category; Hello Guys, Does any have a regex for email validation? I need to allow only period and underscore in the local part , we would need a @ and .com or watever for domain. thank you...

Go Back   Application Development Forum > Programming Languages > PHP

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-27-2008, 03:09 AM
VamVan
Guest
 
Default Regex for email validation

Hello Guys,

Does any have a regex for email validation? I need to allow only period and
underscore in the local part , we would need a @ and .com or watever for
domain.

thank you

Reply With Quote
  #2  
Old 08-27-2008, 03:31 AM
mike
Guest
 
Default Re: [PHP] Regex for email validation

> On 8/27/08, VamVan <vamseevan@gmail.com> wrote:
> Hello Guys,
>
> Does any have a regex for email validation? I need to allow only period and
> underscore in the local part , we would need a @ and .com or watever for
> domain.


php should have a good check built-in.

see http://www.php.net/manual/en/function.filter-var.php

if(!filter_var($var, FILTER_VALIDATE_EMAIL)) {
echo "invalid email";
}

some people also go the extra mile and verify the MX record is valid,
or lookup the MX record and even validate the user exists by querying
the mail server.
Reply With Quote
  #3  
Old 08-27-2008, 03:31 AM
Per Jessen
Guest
 
Default Re: [PHP] Regex for email validation

VamVan wrote:

> Hello Guys,
>
> Does any have a regex for email validation? I need to allow only
> period and underscore in the local part , we would need a @ and .com
> or watever for domain.


Option 1: /^[_.]+@[a-z0-9-]+\.com$/

This is probably what you meant:

/^[a-z0-9_.]+@[a-z0-9-]+\.com$/


/Per Jessen, Zürich

Reply With Quote
  #4  
Old 08-27-2008, 03:35 AM
Micah Gersten
Guest
 
Default Re: [PHP] Regex for email validation

That's a very handy extension.

Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com



mike wrote:
>
> php should have a good check built-in.
>
> see http://www.php.net/manual/en/function.filter-var.php
>
> if(!filter_var($var, FILTER_VALIDATE_EMAIL)) {
> echo "invalid email";
> }
>
> some people also go the extra mile and verify the MX record is valid,
> or lookup the MX record and even validate the user exists by querying
> the mail server.
>
>

Reply With Quote
  #5  
Old 08-27-2008, 04:34 AM
Richard Heyes
Guest
 
Default Re: [PHP] Regex for email validation

> Does any have a regex for email validation? I need to allow only period and
> underscore in the local part , we would need a @ and .com or watever for
> domain.


You could:

1. Take the isValidInetAddress() method out of the PEAR Mail_RFC822
class and use that.
2. Use the filter extension which I understand is now part of PHP
(5.2.x+) This would the preferable option.

--
Richard Heyes

HTML5 Graphing:
http://www.phpguru.org/RGraph
Reply With Quote
  #6  
Old 08-27-2008, 11:15 AM
tedd
Guest
 
Default Re: [PHP] Regex for email validation

At 9:31 AM +0200 8/27/08, Per Jessen wrote:
>VamVan wrote:
>
>> Hello Guys,
>>
>> Does any have a regex for email validation? I need to allow only
>> period and underscore in the local part , we would need a @ and .com
>> or watever for domain.

>
>Option 1: /^[_.]+@[a-z0-9-]+\.com$/
>
>This is probably what you meant:
>
>/^[a-z0-9_.]+@[a-z0-9-]+\.com$/
>
>/Per Jessen, Zürich


Which is probably what you meant:

eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$", $email)

Email comes in different TLD flavors.

But, even that still doesn't cover all the
possible and legal Unicode code-points that can
exist on both sides of the @ of an email address.

For example:

tedd@ˆ.com

is a legal and working email address.

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
Reply With Quote
  #7  
Old 08-27-2008, 12:12 PM
Yeti
Guest
 
Default Re: [PHP] Regex for email validation

<?php
# this one worked fine for me, but it does not cover the full RFC
like: "name" name@notworking.cc OR name <name@notworking.cc>
$regex = "^[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,})$";
if (eregi($regex, $email)) {
// do something
}
# Beware that the filter functions only work under PHP5+. If your PHP
supports them they should be the preferred choice
?>
Reply With Quote
  #8  
Old 08-27-2008, 12:30 PM
Per Jessen
Guest
 
Default Re: [PHP] Regex for email validation

tedd wrote:

>>
>>Option 1: /^[_.]+@[a-z0-9-]+\.com$/
>>
>>This is probably what you meant:
>>
>>/^[a-z0-9_.]+@[a-z0-9-]+\.com$/
>>
>>/Per Jessen, Zürich

>
> Which is probably what you meant:
>
> eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$", $email)
>
> Email comes in different TLD flavors.


Well, I left that for the OP to figure out. Still, your regex is
worse - a domain name cannot contain '%'. The only valid characters
for a domain name are letters, numbers and a hyphen. Also, maximum
length for a domain name is 64 characters, which could/should be
checked too.

> But, even that still doesn't cover all the possible and legal Unicode
> code-points that can exist on both sides of the @ of an email address..


No, they can't. There are no 8-bit characters allowed in an
email-address. Check out RFC2821.

> tedd@ˆ.com
>
> is a legal and working email address.


If that reads "tedd(at)<space>.com", it might be valid on your system,
but not in public.


/Per Jessen, Zürich

Reply With Quote
  #9  
Old 08-27-2008, 12:33 PM
Per Jessen
Guest
 
Default Re: [PHP] Regex for email validation

Yeti wrote:

> <?php
> # this one worked fine for me, but it does not cover the full RFC
> like: "name" name@notworking.cc OR name <name@notworking.cc>
> $regex =
> "^[a-z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-z0-9,!#\$%&'\*\+/=\

\^_`\{\|}~-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,})$";

For the domain part, I would check against

"@([a-z0-9-]+\.)+[a-z0-9-]+"

and then do a lookup for an A record. There are still some patterns
that will fit the above, without being valid domain-names.


/Per Jessen, Zürich

Reply With Quote
  #10  
Old 08-27-2008, 01:55 PM
Lupus Michaelis
Guest
 
Default Re: [PHP] Regex for email validation

mike a écrit :
>
> php should have a good check built-in.
>
> see http://www.php.net/manual/en/function.filter-var.php


Argh ! Howmany times it is in ? I spent so many time to write a regex
that belongs the RFC822 :-/ Because all the regex in answer here was
false. They don't allow email like "Mickael Doodoo"@lupusmic.com nor
mickael+doudou@lupusmic.org ; and they are valuable email addresses.
Without the fact that a top level domain isn't always between two and
three characters (think about .museum).

--
Mickaël Wolff aka Lupus Michaelis
http://lupusmic.org
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 08:01 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.