[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 05-10-2006, 01:32 PM   #1 (permalink)
NamePros Regular
 
moondog's Avatar
 
Join Date: Jun 2004
Posts: 476
3,677.00 NP$ (Donate)

moondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of light


ereg / POSIX question

Trying to do a quick down, dirty, and crude validation of an email address. I have:

PHP Code:
if(ereg('[[:alnum:]]@[[:alnum:]][.][[:alnum:]]', $_POST[email])) { echo "valid email";}
else {echo
"invalid email";}
the probem is that is keeps kicking everything out.

If I remove anything after the period (in the domain) like this:

PHP Code:
if(ereg('[[:alnum:]]@[[:alnum:]]', $_POST[email])) { echo "valid email";}
else {echo
"invalid email";}
it works fine, but I wanted to add the check for the TLD on the domain of the email address. I tried with and without the character class around the period:

PHP Code:
if(ereg('[[:alnum:]]@[[:alnum:]].[[:alnum:]]', $_POST[email])) { echo "valid email";}
else {echo
"invalid email";}
but it still kicks everything out.

Suggestions?

-Bob
__________________
The mass purge has begun.
moondog is offline  
Old 05-10-2006, 02:10 PM   #2 (permalink)
Domains my Dominion
 
sdsinc's Avatar
 
Join Date: Aug 2005
Location: Web 1.0
Posts: 6,285
1,095.94 NP$ (Donate)

sdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond repute

Third World Education Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Animal Rescue Animal Cruelty AIDS/HIV Animal Rescue Wildlife Breast Cancer
Works fine with me. What E-mail address are you using for testing ?
Are you sure the E-mail is not altered in the POST request ?

Also if you want to get extreme you may want to look into raw RFC822 validation:
http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html
but I think it would be overkill ;-)
__________________
Buy now - MassDeveloper.com $500
sdsinc is offline  
Old 05-10-2006, 02:14 PM   #3 (permalink)
Senior Member
 
Shorty's Avatar
 
Join Date: Sep 2005
Location: England
Posts: 1,035
102.05 NP$ (Donate)

Shorty is just really niceShorty is just really niceShorty is just really niceShorty is just really nice


Use this instead:
Code:
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})", $_POST['email']))
Shorty is offline  
Old 05-10-2006, 02:39 PM   #4 (permalink)
NamePros Regular
 
moondog's Avatar
 
Join Date: Jun 2004
Posts: 476
3,677.00 NP$ (Donate)

moondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of light


Thanks shorty.
__________________
The mass purge has begun.
moondog is offline  
Old 05-10-2006, 04:57 PM   #5 (permalink)
NPQ's PA, Slave, and On Call Coder

Technical Services


 
Eric's Avatar
 
Join Date: Mar 2005
Posts: 4,545
0.71 NP$ (Donate)

Eric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond reputeEric has a reputation beyond repute

Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse
Quote:
Originally Posted by Shorty
Use this instead:
Code:
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})", $_POST['email']))
Thought I'd point out, using:
PHP Code:
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})", $_POST['email']))
Would cut out a few extensions

Instead use:
PHP Code:
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})", $_POST['email']))
__________________
Eric is offline  
Old 05-10-2006, 08:40 PM   #6 (permalink)
Senior Member
 
Peter's Avatar
 
Join Date: Nov 2003
Location: Scotland
Posts: 4,900
0.60 NP$ (Donate)

Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute

Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
I personally use :-

PHP Code:
eregi('^[a-z0-9\._-]+@[a-z0-9\._-]+\.[a-z0-9\.]+$',$email_addy,$dummy)
__________________
Manage your portfolio using my new Domain Portfolio Management script.
Securing Your Domain Name From Theft
Peter is offline  
Old 05-11-2006, 09:10 AM   #7 (permalink)
NamePros Regular
 
moondog's Avatar
 
Join Date: Jun 2004
Posts: 476
3,677.00 NP$ (Donate)

moondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of lightmoondog is a glorious beacon of light


Quote:
Originally Posted by SecondVersion
Would cut out a few extensions
Ah, yes, Don't want the .COOP, .AERO, and .MUSEUM folks to feel left out.

-Bob
__________________
The mass purge has begun.
moondog is offline  
Old 05-11-2006, 01:49 PM   #8 (permalink)
NamePros Member
 
Join Date: Apr 2005
Posts: 116
134.00 NP$ (Donate)

mikesherov will become famous soon enoughmikesherov will become famous soon enough


From PHP.net, if you like overkill:
PHP Code:
<?php
// Completely update for match RFC 2822 and RFC 1035
// http://www.faqs.org/rfcs/rfc2822.html
// http://www.faqs.org/rfcs/rfc1035.html

// Example results:

$email[] = 'foo@example.com';                      // matched
$email[] = 'foo.bar@example.co.uk';                // matched
$email[] = 'foo_bar@example.com';                  // matched
$email[] = '_foo_bar@example.com';                // matched
$email[] = 'foo@example.example';                  // matched
$email[] = '%#a+f.*&654_-._@ee.xx';                // matched
$email[] = 'foo@abc-123.xx';                      // matched
$email[] = 'a@a.a.a.a.aa';                        // matched
$email[] = 'a@a9.aa';                              // matched
$email[] = 'a!b#c$d%e^f&g*h\'i+j-k{l|m}n_/@op.qr'; //matched

$email[] = '';                                    //separator

$email[] = 'foo@-example.com';                    // not matched
$email[] = 'foo@example-.com';                    // not matched
$email[] = '%#af.*&@a%#b.xx';                      // not matched
$email[] = 'a@a.99.00.a.aa';                      // not matched
$email[] = '_-._@-.--';                            // not matched
$email[] = 'any..thing@bla.bla';                  // not matched
$email[] = '@.';                                  // not matched
$email[] = '@.com';                                // not matched
$email[] = '@exam@exam.com';                      // not matched
$email[] = ' @ .com';                              // not matched
$email[] = '.bar@example.com';                    // not matched
$email[] = 'foo.@example.com';                    // not matched
$email[] = 'foo@example.x';                        // not matched

$atom = '[-a-z0-9!#$%&\'*+/=?^_`{|}~]';    // allowed characters for part before "at" character
$domain = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)'; // allowed characters for part after "at" character

$regex = '^' . $atom . '+' .        // One or more atom characters.
'(\.' . $atom . '+)*'.              // Followed by zero or more dot separated sets of one or more atom characters.
'@'.                                // Followed by an "at" character.
'(' . $domain . '{1,63}\.)+'.        // Followed by one or max 63 domain characters (dot separated).
$domain . '{2,63}'.                  // Must be followed by one set consisting a period of two
'$';                                // or max 63 domain characters.

foreach ($email as $example) {
   if (
strlen($example) == 0):
       echo
'&nbsp;<br>';
   else:
     if (
eregi($regex, $example)):
       echo
$example . ' matched<br>';
     else:
       echo
'<strong>'. $example . ' not matched</strong><br>';
     endif;
   endif;
}
?>
mikesherov is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 06:22 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85