Last active
August 29, 2015 14:05
-
-
Save phillipsharring/9a4a4f8b28582db5db31 to your computer and use it in GitHub Desktop.
Email Validation Function
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 | |
/** | |
* emailValid | |
* Email Validation Function | |
* I take an email as a string and test it against 2 regular expressions. | |
* | |
* @author Regexp Author unknown | |
* @author Phillip Harrington <philsown@gmail.com> | |
* @param string $email | |
* @return bool $valid | |
*/ | |
function emailValid($email) | |
{ | |
$valid = false; | |
$email = trim($email); | |
if (0 < strlen($email) | |
&& !preg_match('/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/i', $email) | |
&& preg_match('/^.+\@(\[?)[-a-zA-Z0-9\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/i', $email) | |
) { | |
$valid = true; | |
} | |
return $valid; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I used to have the strlen block and the 2 preg if blocks each separately return false throughout, but lately I'm in a single point of exit mindset.
I cannot take credit for the regexps - I borrowed them from someone who borrowed them. If the real author is known, please comment and attribute.
I did however make one edit, which was to increase the TLD portion from 3 chars max to 4 - to account for .info email addresses. One character - my big contribution!
Enjoy!