Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

Xeoncross commented Apr 17, 2014

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

@magnetikonline

This comment has been minimized.

Copy link
Owner Author

magnetikonline commented Apr 17, 2014

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
You can’t perform that action at this time.