Similar to this question, but not sure how to implement in this case.
A trusted user (don't need to be concerned with validating input) is typing/pasting email addresses into a text field. On the blur event, I'd like to look at the text and clean up whatever he inputed (typically after copying and pasting a list of addresses from an email client).
"Bob Smith" <bob@company.com>, joe@company.com, "John Doe"<john@company.com>
would be trimmed to:
bob@company.com, joe@company.com, john@company.com
This regex should remove anything in double-quotes as well as
<
and>
characters.In Javascript, you might have something along these lines:
Valid email address can be very strange, so I'd suggest to not forbidding anything in that field otherwise may be well possible that your program is useless because your users will not be able to send email to valid email addresses.
To read the whole story see this blog post or go for the RFC yourself.
You can use the
.math()
method to quickly parse out the emails into an array:If you want to then convert that to a string, you can add
.join(', ')
or.join('; ')
to it. The regex is simplified. There are quite a few regular expressions out there to parse emails with, but the one above is a simplified version. It does not take into account subdomains, as pointed out in the comments below, or multipart TLDs (It also doesn't take into account the+
symbol in the first part of the email address). Substitute with a regular expression that matches your needs.Or just do the first line if you're wanting an array of the email addresses.
This allows the email list to be provided in the following formats (including copy and paste email list from you Google To box):
The email Ids can be separated by
,
,;
, or newlines.