rfc-compliant javascript email regex


by D. Christoff - fragged.org - updated 24/06/2009
The proof:


The reason: email format conventions and RFCs

The source:
var data = [
    '"Abc\@def"@example.com',
    '"Fred Bloggs"@example.com',
    '"Joe\\Blow"@example.com',
    '"Abc@def"@example.com',
    'customer/department=shipping@example.com',
    '$A12345@example.com',
    '!def!xyz%abc@example.com',
    '_somename@example.com'
], validEmailRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

String.prototype.isEmail = function() {
    return this.match(validEmailRegex);
}

var count = data.length;
while (count--) {
    if (!data[count].isEmail())
        document.write("INVALID: the email " + data[count] + " incorrectly reported as non-valid
"); else document.write("OK: the email " + data[count] + " recognised correctly
"); };