Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Form testing

Posted on 2004-10-22
14
Medium Priority
?
225 Views
Last Modified: 2008-03-06
Hi all


Can anybody tell me to test for validity of an email address?


I mean, can one detect if an address is bogus without actually sending mail to it?


For starters, I need to see if it's in the right format ie user@some-server.somewhereelse.com.us  

And if the address isn't valid, I want to

Redirect to the referer with posted 'frm_Fail_Email=true'

so that I can retrieve this with $post("frm_Fail_Email')
0
Comment
Question by:x_terminat_or_3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 12

Expert Comment

by:minichicken
ID: 12380695
- check an email address validity
- @access public
- @param string $address : email address to check
- @return true if email adress is ok

function ValidEmail($address)
{
      if( ereg( ".*<(.+)>", $address, $regs ) ) {
            $address = $regs[1];
      }
       if(ereg( "^[^@  ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address) )
             return true;
       else
             return false;
}
0
 
LVL 12

Expert Comment

by:minichicken
ID: 12380729
So in your example:

**********************************************************

function ValidEmail($address)
{
     if( ereg( ".*<(.+)>", $address, $regs ) ) {
          $address = $regs[1];
     }
      if(ereg( "^[^@  ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address) )
           return true;
      else
           return false;
}

if (ValidEmail("user@some-server.somewhereelse.com.us  "))
{
       //if VALID do something
}
else
{
      //else INVALID do something else
}
0
 
LVL 8

Expert Comment

by:sigmacon
ID: 12380738
// assuming email address is in $email
if (!ereg('^[A-Za-z0-9_\.\-]+\@([A-Za-z0-9][A-Za-z0-9\-]*\.)+[a-zA-Z]{2,3}$', $email)) {
   // do your redirect here
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:x_terminat_or_3
ID: 12381011
'k sigmacon, I think I'll go for your example as testing for extension (net|com...) won't work because new extensions are created every day.


What about the redirecting?

If invalid email, I want to redirect to the referer page, with the name/value pair 'frm_Fail_Email=true' in the postdata
0
 
LVL 8

Expert Comment

by:sigmacon
ID: 12381201
// the value will be in $_GET, not in $_POST !!!

if (!ereg('^[A-Za-z0-9_\.\-]+\@([A-Za-z0-9][A-Za-z0-9\-]*\.)+[a-zA-Z]{2,3}$', $email)) {
   header('Location: form.php?frm_Fail_Email=true');
}
0
 
LVL 8

Assisted Solution

by:sigmacon
sigmacon earned 600 total points
ID: 12381302
I have to update: You SHOULD NOT continue execution of that file after sending the header and you want the referrer. If this code happens later down the page, you might want to use output buffering (ob_start()) to prevent headers from being sent too early.

if (!ereg('^[A-Za-z0-9_\.\-]+\@([A-Za-z0-9][A-Za-z0-9\-]*\.)+[a-zA-Z]{2,3}$', $email)) {
   header('Location: ' . (isset($_SERVER['HTTP_REFERER'])? $_SERVER['HTTP_REFERER'] : 'form.php') . '?frm_Fail_Email=true');
   die();
}
0
 
LVL 2

Author Comment

by:x_terminat_or_3
ID: 12381781
Sig

I see you added a test to see if the referer is set.  I what case are they empty, asides from when the page's url is typed in the addressbar?
0
 
LVL 5

Accepted Solution

by:
basiclife earned 800 total points
ID: 12382108
I hate posting links but http://www.zend.com/zend/spotlight/ev12apr.php also includes code for connecting to the mail server and verifying the email account is valid
0
 
LVL 12

Assisted Solution

by:minichicken
minichicken earned 600 total points
ID: 12382634
Hi x_terminat_or_3

I would like to know if my function worked for you?
If you would like to do a redirect with a variable, then like sigmacon said you can only use $_GET and not $_POST
So it will be something like this:

***********************************************

function ValidEmail($address)
{
     if( ereg( ".*<(.+)>", $address, $regs ) ) {
          $address = $regs[1];
     }
      if(ereg( "^[^@  ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address) )
           return true;
      else
           return false;
}

if (ValidEmail("user@some-server.somewhereelse.com.us  "))
{
       header('Location: valid_page.php?frm_Fail_Email=true'); //redirect to page stating the email is true (valid)
       exit;
}
else
{
      header('Location: valid_page.php?frm_Fail_Email=false'); //redirect to page stating the email is false (invalid)
      exit;
}

NOTE: remember to use "exit;" after redirecting using header to exit script or else it will execute anything after it, which may cause an error

regards-
0
 
LVL 2

Author Comment

by:x_terminat_or_3
ID: 12382723
Hi chicken

I didn't test your example because I don't believe that it would be wise to test for .net,.com, .gov,.mil,... because those are not constants!

But don't worry, I appreciate your input and will reward (as usual) anybody who a/is part of the solution b/thought me something I didn't know already.


x_terminat_or_3 (Ramses)
0
 
LVL 2

Author Comment

by:x_terminat_or_3
ID: 12383022
basiclife  that's good!
0
 
LVL 8

Expert Comment

by:sigmacon
ID: 12383296
x_terminat_or_3

As pointed out in the comments to the link given by basiclife, this form of verification is not very reliable. I have actually written a similar email verification a while ago, and could have posted it here, but that's was not exactly what your question seemed to be. Anyway: Since I have done this type of 'online' email address verification, I can tell you that the hassle and time involved is not worth it. When people fake email addresses, they usually use well-know providers, such as AOL, Yahoo or Hotmail/MSN. The verification for Hotmail works actually pretty well, but for AOL not at all and for Yahoo it depends on which server you're talking to. Also, finding the mail exchangers, connecting to them and performing the communication can easily take 3, 4 or even more than 10 seconds, even if you're in a very fast datacenter (on a very fast connection). I have tried it - this method is unusable for form field email address validation, the wait time and potential failure rate scares too many users away. Just my 2 cents.
0
 
LVL 2

Author Comment

by:x_terminat_or_3
ID: 12383412
Hi sigma

Your 2cents are noted AND appreciated.  

If you send a form, one can accept a 10sec delay, and if the user has no malintention, then it will work like a charm!

0
 
LVL 5

Expert Comment

by:basiclife
ID: 12383717
Glad I could help 8-D
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question