Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magnetikonline/10965895 to your computer and use it in GitHub Desktop.
Save magnetikonline/10965895 to your computer and use it in GitHub Desktop.
Very liberal PHP is valid email check.
<?php
function isValidEmailAddress($email) {
return (
(preg_match('/^[^\r\n\t]+@[^\r\n\t ]+\.(?i)[a-z]{2,10}$/',$email)) &&
(count($partList = explode('@',$email)) == 2) &&
(!preg_match('/\.{2}/',$partList[1]))
);
}
// valid
isValidEmailAddress("my.name@somedomain.com");
// invalid
isValidEmailAddress("my.n\t\r\name@some\rdomain.com"); // \r\n\t characters bad
isValidEmailAddress("my.name@some domain.com"); // spaces in domain component
isValidEmailAddress("my.name@som@edomain.com"); // multiple "@" characters
isValidEmailAddress("my.name@som@edomain.longtld12334"); // longer than 10 characters TLD
isValidEmailAddress("my.name@some..domain.com"); // double (or more) sequential dots
@xeoncross
Copy link

What about control characters or whitespace? Look at the PHP unicode regex rules on php.net

@magnetikonline
Copy link
Author

Whitespace in the domain portion, good point - but it IS valid in the name@ part if the name is quoted.

Trying not to be too intense here with the checking :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment