Often in writing web applications, one is in the position of having to validate and parse something like:
Charlie C <charlie@charlie-charlie.org>
One wishes to, in addition to verifying that the text represents a valid "jumble of emails", extract a list of the addresses:
Below I provide a javascript routine that does exactly this. Perhaps you will find it useful.
One of the reasons this task is not straightforward is because emails can come in a few formats. Let's (briefly) consider the W3C specification:
mailbox = addr-spec OR phrase route-addr
group = phrase ":" [#mailbox]";" (where "#mailbox" means one or more 'mailbox' tokens)
In other words an "email address" can appear as one of the "mailbox" types: either an addr-spec (Address Specification), e.g. "john@smith.com", a phrase route-addr (Phrase Route Address Specification), e.g. "John Smith <john@smith.com>", OR it can appear as a group, e.g.:
The W3C spec of course breaks down each of the above keywords like "phrase", "addr-spec", and "route-addr" into totally unambiguous terms which the interested reader can check out. I think what appears in most people's minds when they think of "email address" however is the mailbox types, Address Specification and Phrase Route Specification.
The problem we would like to solve is: given some arbitrary sequence of Address Specification and Phrase Route Specification email addresses, do two things:
- Make sure valid email addresses are specified. This means that in addition to meeting the W3C spec, the top level domains, for instance, make sense (e.g. you can't have "john@yourface.yourface" as your address, even though it meets the W3C spec)
- Produce a list of Address Specification addresses.
For example, given the text string:
OR
OR
Strong Bad <strongbad@homestarrunner.com>'
turn it into:
In the file linked below is the javascript for you to do exactly this. It is composed of the following two functions:
- function checkIfValidJumbleOfEmails(aString):
If it's a valid jumble of emails (meaning some sequence of Address or Phrase Route Specification emails separated by either whitespace, a comma, or a semicolon) returns true. Else, returns false.
- function stringToEmailList(aString):
Assuming 'aString' is a "valid jumble of emails", extract and returns them as a list of Address Specification emails.
The javascript is tested to work in IE, Firefox, Safari, and Chrome.