JavaScript function to check an email address conforms to RFC822

A number of times I have been frustrated with simplistic verification of email addresses, not so much because they are incomplete, but because they actually get it wrong.

I did at one stage make a serious attempt understanding and implementing the perl regular expression for checking emails (in accordance with the internet specification RFC822) that is found in the Perl Cookbook, however after reading Cal Henderson's excellent write-up on Parsing Email Adresses in PHP, I realise why it wasn't a helpful place to start (it incorporates more than just the address spec).

So with Cal's advice there is no excuse for web developers to continue to guess at what an email address should be, just do it to the spec (RFC822)!

The original is in PHP, and others have rewritten it for Ruby, and Python...

Here is my contribution, a JavaScript version:

(Licensed under a Creative Commons Attribution-ShareAlike 2.5 License, or the GPL)

function isRFC822ValidEmail(sEmail) {

  var sQtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
  var sDtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
  var sAtom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
  var sQuotedPair = '\\x5c[\\x00-\\x7f]';
  var sDomainLiteral = '\\x5b(' + sDtext + '|' + sQuotedPair + ')*\\x5d';
  var sQuotedString = '\\x22(' + sQtext + '|' + sQuotedPair + ')*\\x22';
  var sDomain_ref = sAtom;
  var sSubDomain = '(' + sDomain_ref + '|' + sDomainLiteral + ')';
  var sWord = '(' + sAtom + '|' + sQuotedString + ')';
  var sDomain = sSubDomain + '(\\x2e' + sSubDomain + ')*';
  var sLocalPart = sWord + '(\\x2e' + sWord + ')*';
  var sAddrSpec = sLocalPart + '\\x40' + sDomain; // complete RFC822 email address spec
  var sValidEmail = '^' + sAddrSpec + '$'; // as whole string
  
  var reValidEmail = new RegExp(sValidEmail);
  
  if (reValidEmail.test(sEmail)) {
    return true;
  }
  
  return false;
}

Download the attached JavaScript file, which also has a basic example.

Hope somebody finds this useful.

AttachmentSize
rfc822validemail.js.txt3.42 KB

Comments

Thank you for your function, but are email addresses without a domain extension valid?

When I enter for example me@somewhere without a domain extension it returns true.

According to the spec, yes, such an email would be valid. However, just because it is a 'valid' email address, that doesn't mean that the email address actually exists!

To take your example (me@somewhere): If you had a company network with email servers set up, and the host name for one of the email servers is 'somewhere', then email sent to me@somewhere would go to an account 'me' on the server 'somewhere'.

Likewise: jdir830dldk30@hotmail.com is a valid email address, but it may not exist either.

If you want to check if an email address is exists, then you need to send a query to the mail server (if it resolves) - but that is a whole different issue! (google it)

Also note: When I wrote the article I didn't realise that RFC822 was out of date, oops! Try RFC 2822 and RFC 2821 instead. I have been meaning to re-write the script but haven't had the chance. That said, I don't think there is much in the way of a functional difference for this part of the spec.

Hi Ross,

First of all, thank you very much for sharing this with everybody. I really appreciate it.

Now, would it be too hard to change this function to validate only domains with 2 or more parts? I know RFC 2822 allows domains composed by only 1 part, but I need to now allow those but to allow all the other characters and stuff that the RFC 2822 allows for the local part.

I'd change it myself, but honestly, I don't understand this regular expression :-S

Thanks.

What exactly is wrong with this solution? It looks like it does the same as Les HazleWood's Java solution. (http://www.leshazlewood.com/?p=23)