Current location: Hot Scripts Forums » Programming Languages » PHP » If email has an "@" and a "." do this else...


If email has an "@" and a "." do this else...

Reply
  #1 (permalink)  
Old 07-23-04, 04:42 AM
dihan dihan is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 267
Thanks: 0
Thanked 0 Times in 0 Posts
If email has an "@" and a "." do this else...

Hi guys,

I have a form which send the info to a php page.

I wanted a way where if $email had an "@" and a "." to run the rest of the script else echo 'No email was given'

I know its somthing like this but not sure exactly how to do it.
PHP Code:

if (isset($_GET['email'])) 


     
$email $_GET['email'];

else 
{
     echo 
'No email was given'
}
//Rest of the script here 
How do I put the @ and . thing in there
Reply With Quote
  #2 (permalink)  
Old 07-23-04, 05:13 AM
<?Wille?> <?Wille?> is offline
Junior Code Guru
 
Join Date: Jan 2004
Location: Helsinki, Finland
Posts: 666
Thanks: 0
Thanked 0 Times in 0 Posts
you can use ereg() function..
Reply With Quote
  #3 (permalink)  
Old 07-23-04, 08:42 AM
MrKafaaski's Avatar
MrKafaaski MrKafaaski is offline
Newbie Coder
 
Join Date: Jul 2004
Location: Hurum, Norway
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Or you can use a quicker method, strstr()
PHP Code:

<?php

if (strstr($email"@") && strstr($email"."))
{
echo 
"Your e-mail looks fine";
}
Reply With Quote
  #4 (permalink)  
Old 07-23-04, 10:38 AM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
With that method, '@.' or '.@' will check as correct.

You can check for correct email syntax (xxx@xxx.xxx, xxx@xxx.xx.xx, etc...) with this (the email address is stored as $email):
PHP Code:

<?


if (ereg("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"$email))
echo 
'Valid email address';
else
echo 
'Invalid email address';

?>
Note that this will not check whether or not the address actually exists!
Reply With Quote
  #5 (permalink)  
Old 07-23-04, 10:42 AM
MrKafaaski's Avatar
MrKafaaski MrKafaaski is offline
Newbie Coder
 
Join Date: Jul 2004
Location: Hurum, Norway
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Keith
With that method, '@.' or '.@' will check as correct.

You can check for correct email syntax (xxx@xxx.xxx, xxx@xxx.xx.xx, etc...) with this (the email address is stored as $email):
PHP Code:

<?


if (ereg("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"$email))
echo 
'Valid email address';
else
echo 
'Invalid email address';

?>
Note that this will not check whether or not the address actually exists!
Yes, you are right, I'm just not to good at regexpressions.
Reply With Quote
  #6 (permalink)  
Old 07-24-04, 09:02 AM
dihan dihan is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 267
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks guys it works perfactly
Reply With Quote
  #7 (permalink)  
Old 07-24-04, 09:18 AM
mikaelf mikaelf is offline
Wannabe Coder
 
Join Date: Jun 2004
Location: php[dot]net
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
yep. it will work perfectly unless you never know whether the username and/or domain name exists. if you want to get REAL email address (based on DNS resolver), you may take a look at PEAR/network. Anyway, if you don't really care whether users submit fake or real email addresses, simple regex check is enough.
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


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