PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
hame22
Forum Contributor
Posts: 214 Joined: Wed May 11, 2005 5:50 am
Post
by hame22 » Mon May 08, 2006 6:12 am
Hi
In my member-based website I am trying to prevent users signing up using email addresses from hotmail and yahoo so as to prevent fraud.
My current email validation code is:
Code: Select all
elseif(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
How would I need to change this to allow this validation?
thanks in advance
dibyendrah
Forum Contributor
Posts: 491 Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:
Post
by dibyendrah » Mon May 08, 2006 6:30 am
The following code will prevent the uses from posting the hotmail or yahoo emails :
Code: Select all
elseif(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[hotmail|yahoo]\.com)$", $email))
Not tested! But it might work ..
Last edited by
dibyendrah on Mon May 08, 2006 11:50 pm, edited 1 time in total.
Roja
Tutorials Group
Posts: 2692 Joined: Sun Jan 04, 2004 10:30 pm
Post
by Roja » Mon May 08, 2006 9:01 am
I would split it into two seperate goals: One to validate the format of the email correctly, and one to flag hotmal and yahoo as invalid.
I have
a ready made email validation function if you'd like to use it. Its RFC compliant, and its an exhaustive check.
For the hotmail and yahoo checks, just do a substr:
Code: Select all
if (strpos($email, "hotmail") || strpos($email, "yahoo))
{
// Mail is invalid.
}
Last edited by
Roja on Mon May 08, 2006 12:23 pm, edited 1 time in total.
hame22
Forum Contributor
Posts: 214 Joined: Wed May 11, 2005 5:50 am
Post
by hame22 » Mon May 08, 2006 10:43 am
Iv tried the sub_str approach but it does not accept any email addresess, any ideas why
here is my code:
Code: Select all
elseif(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
{
$error = "2";
}
//elseif(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[hotmail|yahoo]\.com)$", $email))
elseif (substr($email, "hotmail") || substr($email, "yahoo"))
{
// Mail is invalid.
$error = "10";
}
aerodromoi
Forum Contributor
Posts: 230 Joined: Sun May 07, 2006 5:21 am
Post
by aerodromoi » Mon May 08, 2006 11:21 am
hame22 wrote: Iv tried the sub_str approach but it does not accept any email addresess, any ideas why
I prefer using eregi
Code: Select all
<?php
$email = "abc@testaddress.com";
$error1 = false;
$error2 = false;
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$", $email)) { $error1 = true; }
if(eregi("hotmail",$email) || eregi("yahoo",$email)) { $error2 = true; }
if (!$error1 && !$error2) { echo "congrats!"; }
if ($error1) { echo "invalid email address<br />";}
if ($error2) { echo "yahoo / hotmail address used";}
?>
aerodromoi
btw: there are also top level domains like "museum"...
Last edited by
aerodromoi on Mon May 08, 2006 1:02 pm, edited 1 time in total.
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Mon May 08, 2006 11:36 am
hame22 wrote: Iv tried the sub_str approach but it does not accept any email addresess, any ideas why
here is my code:
Code: Select all
elseif(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
{
$error = "2";
}
//elseif(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[hotmail|yahoo]\.com)$", $email))
elseif (substr($email, "hotmail") || substr($email, "yahoo"))
{
// Mail is invalid.
$error = "10";
}
That should strpos() not substr(). substr() is for pulling out substrings and the second two paramters are integer values.
Roja
Tutorials Group
Posts: 2692 Joined: Sun Jan 04, 2004 10:30 pm
Post
by Roja » Mon May 08, 2006 12:29 pm
hame22 wrote: Iv tried the sub_str approach but it does not accept any email addresess, any ideas why
As d11 pointed out, I mixed up my functions. It should be strpos, NOT substr. I corrected my earlier post.
Sorry, its a Monday.
dibyendrah
Forum Contributor
Posts: 491 Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:
Post
by dibyendrah » Tue May 09, 2006 12:54 am
My alternate approach but similar to the above solutions:
Code: Select all
<?php
$email = "someone@yahoo.com";
$ban_email_domain1 = "yahoo.com";
$ban_email_domain2 = "hotmail.com";
if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
if(strstr($email, $ban_email_domain1)){
print "yahoo.com not allowed";
}elseif (strstr($email, $ban_email_domain2)){
print "hotmail.com not allowed";
}else {
print "valid email with no yahoo or hotmail.com";
//process the member registration
}
}
?>
Cheers,
Dibyendra
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Tue May 09, 2006 1:07 am
Step 1. Vlidate the email adress using regex (like you are doing already).
Step 2. If the email validation passes, strpos it for '@yahoo.com' and '@hotmail.com'
The reason I would throw in the '@' sign is just in case someone has gotten cute and created a website like
http://www.ihateyahoo.com . Their email address would be bounced from your system. If you want to keep out the yahoo.com and hotmail.com domains only, strpos the email address with the '@' sign.
Just my $0.02.
dibyendrah
Forum Contributor
Posts: 491 Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:
Post
by dibyendrah » Tue May 09, 2006 1:20 am
Everah wrote: Step 1. Vlidate the email adress using regex (like you are doing already).
Step 2. If the email validation passes, strpos it for '@yahoo.com' and '@hotmail.com'
The reason I would throw in the '@' sign is just in case someone has gotten cute and created a website like
http://www.ihateyahoo.com . Their email address would be bounced from your system. If you want to keep out the yahoo.com and hotmail.com domains only, strpos the email address with the '@' sign.
Just my $0.02.
Good Logic ! I liked that approach rather than mine using str_str
Thanks Everah !
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Tue May 09, 2006 1:37 am
Every 1 out of 900 or so I hit a homerun.
Glad I could help.