Valid email address

This page is no longer maintained. The script on it is incorrect and I know too little about regular expressions to maintain it. If you wish to rewrite it, go ahead.

Many thanks to Joost Diepenmaat for providing the regular expression and solving an iCab problem. Many thanks to Arjan van de Ven for some technical advice on valid mail addresses.

A reader has translated this page into Italian.

This tiny script checks if a user has entered a valid mail address. Of course the script does not check if the address actually exists, it merely makes sure that the address has the correct syntax and could exist.

It uses Regular Expressions, which are not yet explained on my site. You won't learn using them by reading this page. If you don't understand them, just say "Yeah, right" and copy the script. It works.

Example

Try it by filling in a mail address and hitting 'Check'.


The script

This script is executed when you press the button:

function checkMail()
{
	var x = document.forms[0].email.value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) alert('YES! Correct email address');
	else alert('NO! Incorrect email address');
}

Explanation

First take the data

function checkMail()
{
	var x = document.forms[0].email.value;

The regular expression

Then define the regular expression we need.

	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

The regular expression does the following: it wants to scan the entire value from the beginning of the line (^) to its end ($):

/^ [stuff] $/

The address should start with a bunch of alphanumerical (letters or numbers), underscores, dots or dashes. This is the user name.

([a-zA-Z0-9_\.\-])+

Then comes an @.

\@

After that comes the domain name, which may include several sub-domains (mail.international.company.). Therefore we now allow several series of alphanumerical characters and dashes, followed by a dot.

(([a-zA-Z0-9\-])+\.)+

Finally the top level domain, once again we check for alphanumerical characters, but now without the dash. Also, a top level domain name must be between 2 and 4 characters.

([a-zA-Z0-9]{2,4})+$/;

(Why not simply check for three or four letters as the top level domain name? Because IP addresses are also valid domains, an address could be me@187.223.45.119).

Using it

Now we check if the value of the input field actually matches this regular expression. This is done by the test() method that is automatically available for each regular expression. If the test succeeds (if the address is formed according to the rules set out above), it returns true, else it returns false.

	filter.test(x)

I added an alert based on this return value.

	if (filter.test(x)) alert('YES! Correct email address');
	else alert('NO! Incorrect email address');

Of course you have to insert this script into your own form validator. But these are the basics.

Strange mail addresses

When I'd written this script I asked the advice of a friend who has administered mail servers. He let me know that the script as such is good enough, but that there are some rare cases that aren't adequately covered by it:

First of all, if the user name (left of the @) is between quotes "", a lot more is allowed. As far as I've understood this

"J. P. 's-Gravezande, a.k.a. The Hacker!"@server.com

is a valid email address, since all strange characters are between quotes. However, this syntax is rarely used. I didn't know it was allowed until my friend told me, and I guess most people don't know about it.

Also, the strict syntax for addresses with IP numbers in them is

me@[187.223.45.119]

However, it seems that even some mail servers don't know about this syntax and don't correctly deliver mails sent to such addresses. It is rarely used.

Finally, historically a domain name may not begin with a number, but in practice this rule is disregarded. So my script (and mail servers) will accept

someone@123.com

as a valid mail address, even though strictly speaking it's not allowed.

My script doesn't take this unusual syntax into account. Therefore the conclusion must be that this script accurately checks about 99.5% of the email addresses, so it's not completely fail safe. It will serve for common situations, however, since about 99.9% of the users are unaware of these possibilities and have a normal address.