Featured Posts

Adding HTML Signature to your iPhone 4 without Jailbreaking itAdding HTML Signature to your iPhone 4 without Jailbreaking... One of the features missing on the iPhone is HTML signature for Mail, and if you are using two different email addresses like me you probably want to have two different...

Readmore

Play Tetris Using Mac Leopard TerminalPlay Tetris Using Mac Leopard Terminal Terminal Tetris is actually part of the Gnu Emacs and is something you should able to do on any unix system that has Emacs on it. I attached text and video instructions. To...

Readmore

Automate a Remote Login Using SSH - LinuxAutomate a Remote Login Using SSH - Linux To do just that you need to create SSH Key Pair 1. we need to use ssh-agent to create an SSH key pair. To do that run this command: ssh-keygen -t rsa When prompted...

Readmore

Free Google Wave InvitesFree Google Wave Invites Do you really want a Google Wave Invite but don't want to pay $$$ on eBay??? I got one for you.... The deal is: I give you an Invitation and once you get it...

Readmore

Start Google Chrome in Incognito ModeStart Google Chrome in Incognito Mode I'm Using Mozilla FireFox as my default web browser But I like using Chrome for Private Browsing. Google Chrome doesn't provide an option to load Incognito by default so...

Readmore

Cool Geex Rss

PHP Email Address Validator

Posted on : 08-10-2009 | By : Cool Geex | In : PHP / MySql

0

email-validator

Few Years ago I wrote an email validator function for sites forms, one that I used until I found email address validator from Google Code.Enjoy the code by clicking the download now button.

[PHP]check_text_length($strEmailAddress, 3, 256)) {
return false;
}

// Split it into sections using last instance of “@”
$intAtSymbol = strrpos($strEmailAddress, ‘@’);
if ($intAtSymbol === false) {
// No “@” symbol in email.
return false;
}
$arrEmailAddress[0] = substr($strEmailAddress, 0, $intAtSymbol);
$arrEmailAddress[1] = substr($strEmailAddress, $intAtSymbol + 1);

// Count the “@” symbols. Only one is allowed, except where
// contained in quote marks in the local part. Quickest way to
// check this is to remove anything in quotes. We also remove
// characters escaped with backslash, and the backslash
// character.
$arrTempAddress[0] = preg_replace(‘/./’
,”
,$arrEmailAddress[0]);
$arrTempAddress[0] = preg_replace(‘/”[^"]+”/’
,”
,$arrTempAddress[0]);
$arrTempAddress[1] = $arrEmailAddress[1];
$strTempAddress = $arrTempAddress[0] . $arrTempAddress[1];
// Then check – should be no “@” symbols.
if (strrpos($strTempAddress, ‘@’) !== false) {
// “@” symbol found
return false;
}

// Check local portion
if (!$this->check_local_portion($arrEmailAddress[0])) {
return false;
}

// Check domain portion
if (!$this->check_domain_portion($arrEmailAddress[1])) {
return false;
}

// If we’re still here, all checks above passed. Email is valid.
return true;

}

/**
* Checks email section before “@” symbol for validity
* @param strLocalPortion Text to be checked
* @return True if local portion is valid, false if not
*/
protected function check_local_portion($strLocalPortion) {
// Local portion can only be from 1 to 64 characters, inclusive.
// Please note that servers are encouraged to accept longer local
// parts than 64 characters.
if (!$this->check_text_length($strLocalPortion, 1, 64)) {
return false;
}
// Local portion must be:
// 1) a dot-atom (strings separated by periods)
// 2) a quoted string
// 3) an obsolete format string (combination of the above)
$arrLocalPortion = explode(‘.’, $strLocalPortion);
for ($i = 0, $max = sizeof($arrLocalPortion); $i < $max; $i++) { if (!preg_match(‘.^(‘ . ‘([A-Za-z0-9!#$%&'*+/=?^_`{|}~-]‘ . ‘[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]{0,63})’ .’|’ . ‘(“[^"]{0,62}”)’ .’)$.’ ,$arrLocalPortion[$i])) { return false; } } return true; } /** * Checks email section after “@” symbol for validity * @param strDomainPortion Text to be checked * @return True if domain portion is valid, false if not */ protected function check_domain_portion($strDomainPortion) { // Total domain can only be from 1 to 255 characters, inclusive if (!$this->check_text_length($strDomainPortion, 1, 255)) {
return false;
}
// Check if domain is IP, possibly enclosed in square brackets.
if (preg_match(‘/^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])’
.’(.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}$/’
,$strDomainPortion) ||
preg_match(‘/^[(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])’
.’(.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}]$/’
,$strDomainPortion)) {
return true;
} else {
$arrDomainPortion = explode(‘.’, $strDomainPortion);
if (sizeof($arrDomainPortion) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0, $max = sizeof($arrDomainPortion); $i < $max; $i++) { // Each portion must be between 1 and 63 characters, inclusive if (!$this->check_text_length($arrDomainPortion[$i], 1, 63)) {
return false;
}
if (!preg_match(‘/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|’
.’([A-Za-z0-9]+))$/’, $arrDomainPortion[$i])) {
return false;
}
if ($i == $max – 1) { // TLD cannot be only numbers
if (strlen(preg_replace(‘/[0-9]/’, ”, $arrDomainPortion[$i])) <= 0) {
return false;
}
}
}
}
return true;
}

/**
* Check given text length is between defined bounds
* @param strText Text to be checked
* @param intMinimum Minimum acceptable length
* @param intMaximum Maximum acceptable length
* @return True if string is within bounds (inclusive), false if not
*/
protected function check_text_length($strText, $intMinimum, $intMaximum) {
// Minimum and maximum are both inclusive
$intTextLength = strlen($strText);
if (($intTextLength < $intMinimum) || ($intTextLength > $intMaximum)) {
return false;
} else {
return true;
}
}

}
[/PHP]

Related posts:

  1. Create your own list of email addresses from your inbox
  2. Porsche Panamera Flash Drive
  3. Setup VPN Client using Snow Leopard
  4. Google New Programming Language – GO

How To Link To This Page?

To link to this page, simply Copy and Paste the following code to your web page.

Write a comment

Available on the App Store

Advertise Here


Subscribe To CoolGeex