Email
|
|
|
|
<?php // ***********************************************************************
//
// is_valid_email(): an e-mail validation utility routine
// Version 1.1.1 -- September 10, 2000
//
// Written by Michael A. Alderete
// Please send bug reports and improvements to: <michael@aldosoft.com>
//
// This function matches a proposed e-mail address against a validating
// regular expression. It's intended for use in web registration systems
// and other places where the user is inputting their e-mail address and
// you want to check that it's OK.
//
// It does NOT attempt to prove that the address exists, only that it
// COULD exist. Validators which look up the host, try to validate an MX
// record, etc. can significantly slow the response time of your
// application; network connections are significantly more expensive than
// regular expressions, especially on days when the Internet is slow.
//
// It also doesn't attempt to ensure that the TLD is a known valid TLD.
// Doing a lookup against a table isn't too expensive, performance-wise,
// but given the explosion in Internet usage, keeping a lookup table
// up-to-date would be problematic.
//
// The validating regular expression is significantly more accurate than
// many others I've seen in example code. It still won't prevent users
// from entering bogus entries like "a@b.com", but then, if the user
// really doesn't want to give your their e-mail address, there's nothing
// you can do about it, other than make a confirmation e-mail a
// requirement to registering.
//
// You should clean up the input e-mail address in your code before you
// validate it, with trim() and strtolower(), if you're planning to save
// it into a database or otherwise preserve it beyond the one-time use.
// The validator doesn't depend on it, but you'll want to save clean data!
//
// Example of usage: (assumes the user entry form variable is $f_email)
//
// $f_email = strtolower(trim($f_email));
// if (is_valid_email($f_email)) {
// // Do database save here
// // Or send them a confirmation e-mail
// }
//
// Implementation notes:
//
// The routine uses the Perl Compatible Regular Expressions (pcre) routine
// preg_match(), which my benchmarking indicates is about twice as fast
// as the standard ereg() routines in this usage (10 seconds vs. 21
// seconds to do 50,000 validations). If your installation of PHP doesn't
// have the pcre package compiled in, you can switch to using the
// is_valid_email_eregi() version, also provided.
//
// Notes about the expression:
// - The user name needs to be pretty tolerant; you'd be amazed at
// what can be in this portion of a valid address
// - The expression allows an unlimited number of sub-domains, e.g.:
// user@my.really.deep.sub.domain.com is allowed
// - The only four-character TLD that's valid is "arpa", but the
// expression doesn't know that, and allows any four-character TLD.
//
// ***********************************************************************
function is_valid_email ($address) {
return (preg_match(
'/^[-!#$%&'*+\./0-9=?A-Z^_`{|}~]+'. // the user name
'@'. // the ubiquitous at-sign
'([-0-9A-Z]+.)+' . // host, sub-, and domain names
'([0-9A-Z]){2,4}$/i', // top-level domain (TLD)
trim($address)));
}
function is_valid_email_eregi ($address) {
return (eregi(
'^[-!#$%&'*+\./0-9=?A-Z^_`{|}~]+'. // the user name
'@'. // the ubiquitous at-sign
'([-0-9A-Z]+.)+' . // host, sub-, and domain names
'([0-9A-Z]){2,4}$', // top-level domain (TLD)
trim($address)));
}
?>
|
|
|
Usage Example
|
$f_email = strtolower(trim($f_email));
if (is_valid_email($f_email)) {
// Do database save here
// Or send a confirmation e-mail
// Or whatever
}
|
|
|
Rate This Script
|
|
|
|