Skip to content

Core\Validate::isEmail

Checks whether a text is an email address, such as ada@example.com. The method returns true or false.

An address has one @. The part before it has at most 64 characters: English letters, digits, single dots and some symbols such as +, - and _. It does not start or end with a dot. The part after the @ is a domain name with at least one dot, as Core\Validate::isDomain checks it. The whole address has at most 254 characters.

A space, two dots in a row, a letter with an accent or a second @ give false. So does an address without a dot after the @, such as admin@localhost. Core\Validate::isEmail checks only how the address is written. It does not check whether the address exists or can receive email.

The examples below check a few addresses, show the length limit of the part before the @, and check a list of addresses pasted into a form.

Parameters

  • $sstringThe text to test.

Return value

Returns bool. true for an email address. false for anything else, for example a space, two dots in a row, quotes, an address such as a@[192.0.2.1], a domain without a dot such as a@localhost, or a character that is not ASCII. The method does not check whether the address exists.

Examples

Check a few addresses

<?nvs
// Shows which texts are email addresses.
// The first two are addresses. "admin@localhost" has no dot after the `@`.
// The others contain two dots in a row, a space, or no `@` at all.
array<string> $texts = ['ada@example.com', 'ada.lovelace+news@mail.example.org', 'admin@localhost', 'ada..l@example.com', 'ada @example.com', 'example.com'];
// `Core\Validate::isEmail` returns `true` for an email address.
// This prints yes for the first two texts and no for the other four.
foreach ($texts as string $text) {
echo $text, ': ', Core\Validate::isEmail($text) ? 'yes' : 'no', "\n";
}
Output
ada@example.com: yes
ada.lovelace+news@mail.example.org: yes
admin@localhost: no
ada..l@example.com: no
ada @example.com: no
example.com: no

The part before the at sign

<?nvs
// Shows the length limit of the part before the `@`.
// The part before the `@` may have up to 64 characters.
string $name = Core\Str::repeat('a', 64);
string $longer = $name . 'a';
// This prints 64 yes, then 65 no.
echo Core\Str::length($name), ' ', Core\Validate::isEmail($name . '@example.com') ? 'yes' : 'no', "\n";
echo Core\Str::length($longer), ' ', Core\Validate::isEmail($longer . '@example.com') ? 'yes' : 'no', "\n";
Output
64 yes
65 no

Check a pasted list

<?nvs
// Shows how to check a list of addresses that a user pasted into a form.
// The user separated the addresses with commas and added some spaces.
string $pasted = 'ada@example.com, grace@example.org,bob@@example.com , linus@localhost';
array<string> $valid = [];
array<string> $invalid = [];
// `split` makes one text for each address, and `trim` removes the spaces.
foreach (Core\Str::split($pasted, ',') as string $part) {
string $address = Core\Str::trim($part);
if (Core\Validate::isEmail($address)) {
$valid[] = $address;
} else {
$invalid[] = $address;
}
}
// This prints the two valid addresses on the first line and the two others on the second.
echo 'send to: ', Core\Str::join($valid, ', '), "\n";
echo 'please check: ', Core\Str::join($invalid, ', '), "\n";
Output
send to: ada@example.com, grace@example.org
please check: bob@@example.com, linus@localhost