Last active
May 3, 2022 09:06
-
-
Save chazmead/8240317 to your computer and use it in GitHub Desktop.
Email Validation the correct way!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Email Validation | |
* | |
* @access public | |
* @param string | |
* @return bool | |
* | |
* FUNCTION BY CHAZMEAD89 @ GMAIL.COM | |
* | |
****************************************************************************** | |
* Following are the requirements for an e-mail address, with relevant | |
* references: | |
****************************************************************************** | |
* | |
* An e-mail address consists of local part and domain separated by an at sign | |
* (@) character (RFC 2822 3.4.1). | |
* | |
* The local part may consist of alphabetic and numeric characters, and the | |
* following characters: !, #, $, %, &, ', *, +, -, /, =, ?, ^, _, `, {, |, } | |
* and ~, possibly with dot separators (.), inside, but not at the start, end | |
* or next to another dot separator (RFC 2822 3.2.4). | |
* | |
* The local part may consist of a quoted string—that is, anything within | |
* quotes ("), including spaces (RFC 2822 3.2.5). | |
* | |
* Quoted pairs (such as \@) are valid components of a local part, though an | |
* obsolete form from RFC 822 (RFC 2822 4.4). | |
* | |
* The maximum length of a local part is 64 characters (RFC 2821 4.5.3.1). | |
* | |
* A domain consists of labels separated by dot separators (RFC1035 2.3.1). | |
* | |
* Domain labels start with an alphabetic character followed by zero or more | |
* alphabetic characters, numeric characters or the hyphen (-), ending with an | |
* alphabetic or numeric character (RFC 1035 2.3.1). | |
* | |
* The maximum length of a label is 63 characters (RFC 1035 2.3.1). | |
* | |
* The maximum length of a domain is 255 characters (RFC 2821 4.5.3.1). | |
* | |
* The domain must be fully qualified and resolvable to a type A or type MX | |
* DNS address record (RFC 2821 3.6). | |
* | |
****************************************************************************** | |
*/ | |
function valid_email($email) | |
{ | |
$isValid = true; | |
$atIndex = strrpos($email, "@"); | |
if (is_bool($atIndex) && !$atIndex) | |
$isValid = false; | |
else { | |
$split = explode('@', $email); | |
$domain = $split[count($split)-1]; | |
$domainLen = strlen($domain); | |
$local = substr($email, 0, ((strlen($email) - strlen($domain)) - 1)); | |
$localLen = strlen($local); | |
if ($localLen < 1 || $localLen > 64) { | |
// local part length exceeded | |
$isValid = false; | |
} | |
else if ($domainLen < 1 || $domainLen > 255) { | |
// domain part length exceeded | |
$isValid = false; | |
} | |
else if ($local[0] == '.' || $local[$localLen-1] == '.') { | |
// local part starts or ends with '.' | |
$isValid = false; | |
} | |
else if (preg_match('/\\.\\./', $local)) { | |
// local part has two consecutive dots | |
$isValid = false; | |
} | |
else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) { | |
// character not valid in domain part | |
$isValid = false; | |
} | |
else if (preg_match('/\\.\\./', $domain)) { | |
// domain part has two consecutive dots | |
$isValid = false; | |
} | |
else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) { | |
// character not valid in local part unless | |
// local part is quoted | |
if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))) | |
$isValid = false; | |
} | |
// domain not found in DNS | |
if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) | |
$isValid = false; | |
} | |
return $isValid; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
minor edit in argument
function valid_email($email){ ....