473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

e-mail validation

I've been looking for a simple way of checking that a string is an e-mail
address. I don't need to check if the address exists, just if the format of
the string matches. There seem to be lots of different methods, can someone
suggest which is the best, with justification. I'm not looking for total
accuracy, I'd much rather let a few false positives through than get any
false negatives.

Many thanks
Jul 16 '05 #1
4 3822
Hello,

On 08/20/2003 07:14 PM, Geoff Soper wrote:
I've been looking for a simple way of checking that a string is an e-mail
address. I don't need to check if the address exists, just if the format of
the string matches. There seem to be lots of different methods, can someone
suggest which is the best, with justification. I'm not looking for total
accuracy, I'd much rather let a few false positives through than get any
false negatives.


You may want to check these classes:
http://www.phpclasses.org/emailvalidation
It has basic regular expression based validation if you do not want to
use the DNS or SMTP server based validation.

http://www.phpclasses.org/formsgeneration
Also regular expression based validation integrated with forms
generation and validation on the client side using Javascript and on the
server side using the class itself.
--

Regards,
Manuel Lemos

Free ready to use OOP components written in PHP
http://www.phpclasses.org/

Jul 16 '05 #2
> I've been looking for a simple way of checking that a string is an e-mail
address. I don't need to check if the address exists, just if the format of the string matches. There seem to be lots of different methods, can someone suggest which is the best, with justification. I'm not looking for total
accuracy, I'd much rather let a few false positives through than get any
false negatives.

you could use the following regexp for a quick solution with preg_match().
regexp:
'/^([\._a-zA-Z0-9-]{1,}){1}@{1}([a-zA-Z0-9-]{1,}){1}\.{1}([a-zA-Z]{1,3}){1}$
/'

yours, dreamguard.
Jul 16 '05 #3

"Geoff Soper" <ne***********@alphaworks.co.uk> wrote in message
news:3f*********************@news.dial.pipex.com.. .
I've been looking for a simple way of checking that a string is an e-mail
address. I don't need to check if the address exists, just if the format of the string matches. There seem to be lots of different methods, can someone suggest which is the best, with justification. I'm not looking for total
accuracy, I'd much rather let a few false positives through than get any
false negatives.

Many thanks


This is a PHP version of a JavaScript I found that performs syntax checking
on an email address... There is sufficient remarks in it to let you know the
checks that it does - you just pass it an email address and it will return
"TRUE" or "FALSE" pending if its valid or not. Note, I return my TRUE and
FALSE as a string (ie, inside double quotes) and not as a CONSTANT or INT.

Function (and an example of usage) is below:
function verifyEmail($emailAddress)
{
// Return "TRUE" if we believe $email is a valid email address,
// else return "FALSE"

// First - make sure it has an @ sign and ensure that each side
// of the @ sign has enough characters to be a valid address
$pos = strpos($emailAddress, "@");
if ($pos === false) { // note: three equal signs
return("FALSE");
}

list($email, $domain)=explode("@", $emailAddress);
if( (strlen($email)==0) || (strlen($domain)<2) )
{ return("FALSE"); }

// make sure the top level of the domain name is no less than
// two characters, and no greater than four characters in order
// to allow .uk, .com, .info, .net etc...
$domains=explode(".", $domain);

// Make sure the right side of the @ sign (the domain) is made
// up of at least two subdomains (ie @xyz.com and not just @com)
if(count($domains)>1)
{ $tld=array_pop($domains);
// Make sure the top level domain is NOT less than 2 char in length
// and not greater than 4 char in length
if( (strlen($tld)<2) || (strlen($tld)>4) )
{ return("FALSE"); }
}
return("TRUE");
}

$e**********@large.com;
if(verifyEmail($example)=="FALSE")
{
die("Email address $example is NOT valid.");
}
print("<hr>Email address $example is believed to be fine and dandy.<hr>");
Jul 16 '05 #4
"Geoff Soper" <ne***********@alphaworks.co.uk> wrote in message
news:<3f*********************@news.dial.pipex.com> ...

I've been looking for a simple way of checking that a string is an e-mail
address. I don't need to check if the address exists, just if the format of
the string matches. There seem to be lots of different methods, can someone
suggest which is the best, with justification.


It all depends... For example, do you want to support the "user at
domain dot com" syntax? A slightly less wicked one: do you want to
treat '"Geoff Soper" <ne***********@alphaworks.co.uk>' as a valid
e-mail address or do you only want to deal with the 'news.20-08-
03@alphaworks.co.uk' part?

Cheers,
NC
Jul 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Marlon | last post by:
How can modify this expression \w+(\w+)*@\w+(\w+)*\.\w+(\w+)* to validate multiple email address separator by comma and/or semicolon e.g. someone@somedomain.com;someone2@somedomain.com
4
by: VbUser25 | last post by:
Hi Please suggest i think i am doing something wrong. I am calling fucntion test from another function where i am performing all the validations.I want to validate the email id. this is the...
2
by: AFN | last post by:
Has anyone written any code to verify an email address against an SMTP server? I hear you can see if the address is an alias (catchall) versus a real account. I know you can telnet on port 25...
1
by: Jim Dornbush | last post by:
Has anyone seen an updated regex expression from Microsoft for the email validation expression so that single quotes are allowed? I've been using the canned regex for emails, but recently been...
7
by: e_matthes | last post by:
Hello everyone, I've read enough about email validation to know that the only real validation is having a user respond to a confirmation message you've sent them. However, I want to store the...
10
by: ll | last post by:
Hi, I currently am using the following regex in js for email validation, in which the email addresses can be separated by commas or semicolons. The problem, however, lies in that I can type two...
1
by: vimal.424 | last post by:
Hello guys........ please tell me ............. I want to do email validation like i have a userid entered by user new user.so problem is that i have to do the validation when i'll click...
1
by: shwethatj | last post by:
My problem is lik this , I am trying to create a registration form using javascript for a HR website , but i dont know how to provide password validation i.e the password and confirmation password...
1
by: curi444 | last post by:
How can i improve email validation in the code below? <?php $con = mysql_connect("localhost", "root", ""); if(isset($_POST)) { $name=$_POST; $address=$_POST; $phno=$_POST;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.