» Quick Links
You can make money on these forums
We Share profits with you
Google
Google Adsense
Google Adwords
YPN
Yahoo
MSN Search
Web Directories
Web Hosting
Web Hosting Offers
Hosting News
Suggestions
Link Building
Domain Names
PHP Forums
MySQL Forums
» More Links
OSP News
Reseller Hosting
Shared Hosting
Dedicated Servers
Google Adsense
Search Engine Marketing
Link Development
Affiliate Marketing


» Advertising
Multiple DC PR Check

Free SEO Tools


Go Back   Webmaster Forums > Website Developement / Programming > PHP Programming and Tips

PHP Programming and Tips Discuss about PHP programming and Share Tips. Ask questions about Scripting and Errors.

Reply
 
Thread Tools Display Modes
  #1  
Old 08-08-2006, 01:50 PM
nuwan nuwan is offline
Junior Member
 
Join Date: Aug 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
nuwan is on a distinguished road
Default Validating the syntax of an email address

Hi there

Can someone supply with a PHP code that checks whether a given PHP string
constitutes a legal email address in terms of the syntax?
I am sure regular expressions are the way to go.
I found the following solution:

Hi
Hi

function isValidEmailSyntax($email) {
if(ereg("^([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])*$",$email)) {
return true;
} else {
return false;
}
}


But when I pass the value 'nuwan@yahoo.com' it says that the email address is NOT valid.
Does that sound right?
Can anyone see the bug in the function? -- This message may have been cut off and the rest will only be shown to members. To become a member, click here --
Reply With Quote
Revenue Sharing Ads ( ?):
  #2  
Old 08-08-2006, 01:52 PM
gilchrist gilchrist is offline
OSP Starters
 
Join Date: Jul 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
gilchrist is on a distinguished road
Default

TBH, its only really worthwhile checking for the existance of the '@' symbol - if someone wants to give a bad email, they really will, evne if it 'should' exist.

Anyway, something like this ought to hold true for any (current) email addresses:

PHP Code:
function legal_email($email)
{
return ( preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $email)) ? True : False;
}


But its not pretty to look at, far simpler to use something like this (ok, it only shows they're trying to give an email, but thats really the best you should hope for)

PHP Code:
function legal_email($email)
{
return (strchr($email, '@') !== False) ? True : False;
}



as to bugs in your function, you don't need commas between a-z,0-9 etc. Beyond that, it may be that POSIX won't work with it [this is not true, -- This message may have been cut off and the rest will only be shown to members. To become a member, click here --
Reply With Quote
  #3  
Old 08-08-2006, 01:54 PM
gordo gordo is offline
OSP Starters
 
Join Date: Jul 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
gordo is on a distinguished road
Lightbulb

Nuwan

This function has wored fine for me

It checks that an email is the correct format, as definied in the RFC.

PHP Code:
function is_valid_email($email)
{
$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
$atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
'\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
$quoted_pair = '\\x5c\\x00-\\x7f';
$domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
$quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
$domain_ref = $atom;
$sub_domain = "($domain_ref|$domain_literal)";
$word = "($atom|$quoted_string)";
$domain = "$sub_domain(\\x2e$sub_domain)*";
$local_part = "$word(\\x2e$word)*";
$addr_spec = "$local_part\\x40$domain"; -- This message may have been cut off and the rest will only be shown to members. To become a member, click here --
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PIN for an email address needed????// bigdad Web Hosting Industry 1 09-06-2006 07:29 AM
PIN for email address! Aroan Web Hosting Industry 1 08-24-2006 03:49 AM
Grand Canyon Domains Launches Web-Based Email 4.0 outsourcingplans Web Hosting Industry 0 05-09-2006 08:19 PM
Privacy Networks Launches Email Integrity Suite outsourcingplans Web Hosting Industry 0 02-08-2006 06:24 PM
Rockliffe Reduces Complexity of Wireless Email outsourcingplans Web Hosting News 0 12-27-2005 02:29 PM


All times are GMT. The time now is 12:44 PM.