Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] email address problm


[SOLVED] email address problm

Reply
  #1 (permalink)  
Old 12-03-08, 05:08 AM
Miben Miben is offline
Wannabe Coder
 
Join Date: Sep 2008
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] email address problm

$email=$_POST['email'];

How to catch the single quote or double quote appear in $_POST['email']
and prompt error msg to user?
This is to prevent user from enter email address such as jo'n@example.com
or jo"n@example.com

Do i need this kind of validation?

Last edited by Miben; 12-03-08 at 05:11 AM.
Reply With Quote
  #2 (permalink)  
Old 12-03-08, 05:35 AM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
Yes you need to validate if you want to make sure it uses correct syntax. Try something like this

PHP Code:

if(checkEmail($_POST['email'])

{
   
$email=$_POST['email'];
}
else
{
   
//do something else like warn user.
}
function 
checkEmail($email)
{
  
  
// checks proper syntax
  
if( !preg_match"/^([a-zA-Z0-9])+([a-zA-Z0-9\._-+])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/"$email))
  {
    return 
false;
  } 
The + is included in the username part of the emailaddress, because that is allowed. It is not allowed in domain or extension.
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Reply With Quote
  #3 (permalink)  
Old 12-03-08, 08:04 AM
ianbrind ianbrind is offline
Wannabe Coder
 
Join Date: Jul 2008
Location: Somerset UK!
Posts: 192
Thanks: 0
Thanked 0 Times in 0 Posts
This is the code I use.

It checks if the domain is valid.

PHP Code:

function checkEmail($email
{
   if(
eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]"$email)) 
   {
      return 
false;
   }

   list(
$Username$Domain) = split("@",$email);

   if(
getmxrr($Domain$MXHost)) 
   {
      return 
true;
   }
   else 
   {
      if(
fsockopen($Domain25$errno$errstr30)) 
      {
         return 
true
      }
      else 
      {
         return 
false
      }
   }
}

if(
checkEmail($_POST['email']) == false
{
    echo 
"The email address you entered is not valid.";
}
else{
// process data

Reply With Quote
  #4 (permalink)  
Old 12-03-08, 08:06 AM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
Quote:
Originally Posted by ianbrind View Post
This is the code I use.

It checks if the domain is valid.
True and good check, but will NOT work on Windows based webservers (IIS) serving PHP.
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Reply With Quote
  #5 (permalink)  
Old 12-03-08, 07:00 PM
Miben Miben is offline
Wannabe Coder
 
Join Date: Sep 2008
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
if( !preg_match( "/^([a-zA-Z0-9])+([a-zA-Z0-9\._-+])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $email))
{
return false;
}
The above give error on compilation fails..
what wrong with it?

How can i prevent user from entering user@hotmail.com.com
or something else which is not a proper mail hostname?

Last edited by Miben; 12-03-08 at 07:06 PM.
Reply With Quote
  #6 (permalink)  
Old 12-03-08, 08:13 PM
Miben Miben is offline
Wannabe Coder
 
Join Date: Sep 2008
Posts: 146
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks...solved
Reply With Quote
  #7 (permalink)  
Old 12-04-08, 05:04 AM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
Quote:
Originally Posted by Miben View Post
The above give error on compilation fails..
what wrong with it?

How can i prevent user from entering user@hotmail.com.com
or something else which is not a proper mail hostname?
You can use the dns resolving code to check if the domain is valid (hotmail.com.com istnt). But keep In mind that 2 periods in the suffix of the emailaddress is allowed!!

So info@inter.nl.net is a valid email!!
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
User Enters Email Address Before Downloading File bobgreenboston Script Requests 3 05-29-08 04:15 PM
How can I get the email address from a email? Idealws PHP 1 12-04-07 05:23 PM
maintaining the same email address in a form cedric813 Perl 1 11-27-07 12:22 AM


All times are GMT -5. The time now is 08:25 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.