TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-21-2008, 02:16 AM   #1 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 132
Thanks: 3
Gareth is on a distinguished road
Default Simple Email Validation

Hey,

This might seem really noobish, but I wanted to create a simple, but effective email validation function.

Most other functions/classes were too complicated: they did things which I did not need and were way above me. My aim was to make sure that the email a user entered basically was an email.

I would love suggestions on how I could improve it, as I am sure there are improvements to be made!

PHP Code:
function checkEmail($email) {
    if(
eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$"$email)) {
        return 
true;
          }else{
        return 
false;
        }

uses as such:

PHP Code:
$email "gwp123@gwp123.com";

    if (
checkEmail($email)){
         echo 
'its valid!';         
     } else {
        echo 
'its not valid :(';
    } 
This would echo out "its valid!".

Gareth

Last edited by Gareth : 02-21-2008 at 01:22 PM.
Gareth is offline  
Reply With Quote
Old 02-21-2008, 02:32 AM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
Wildhoney is on a distinguished road
Default

I think we had a discussion similar to this before, and the conclusion was that we as programmers should not be using any advanced regex to check the syntax of an email address. After all, email validation would weed out any invalid emails, and then a crontab would come along and clear out any inactive accounts.

However, we should definitely by using a simple regex for checking the very simple structure of any given email address. Such as like:

Code:
^.+?@.+?\..+$
This will prevent people from clearly not entering a valid email address - often by mistake.

Furthermore, being as sure as possible that an email address is valid would be somewhat slow, and that is surely not a favourable sacrifice. After all, email address structures can be very complicated.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is online now  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
Gareth (02-21-2008)
Old 02-21-2008, 02:50 AM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 714
Thanks: 2
Salathe is on a distinguished road
Default

A good first thing to do would be to switch from using eregi to preg_match (with the i modifier). This will improve the running time of the function to around a quarter of that of eregi! Also, this is a person preference more than anything, I don't like to see functions where a conditional is processed which determines a boolean response which is assigned to a local variable. Why not just use a single line of code? return (bool) preg_match(...);

For the regular expression itself, you've made an ok start. However, this falls victim of a hugely common bug in many "validation" checks: it would flag salathe+spam@talkphp.com as invalid, when it's not. You simply need to allow the + character in the appropriate place.

Since you're only checking to see if the pattern matches or not, there's no need to use capturing groups (...) so you can same some time and effort (on the parser's part) by making them non-capturing (?:...).
__________________
Salathe is offline  
Reply With Quote
Old 02-21-2008, 01:23 PM   #4 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 132
Thanks: 3
Gareth is on a distinguished road
Default

Thanks for the input guys! You rock :P

I'm going to look up on preg_match, I don't use it that often so I'll need to remind myself of the syntax and uses etc.

But for now I've updated the function with returning the bools straight off.
Gareth is offline  
Reply With Quote
Reply



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

Advanced Search
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

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


All times are GMT. The time now is 11:56 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0