6

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

Community
  • 1
  • 1
snumpy
  • 2,808
  • 6
  • 24
  • 39

5 Answers5

6

This regex should remove anything in double-quotes as well as < and > characters.

/".*?"|[<>]/

In Javascript, you might have something along these lines:

line.replace(/".*?"|[<>]/g, '');
Mark Biek
  • 146,731
  • 54
  • 156
  • 201
  • It doesn't really matter. The regex above will remove any angle brackets that are present. Nothing will happen to any email addresses that aren't surrounded by angle brackets. – Mark Biek Aug 17 '11 at 20:01
  • Sorry, I missed that this was Javascript and not PHP. I've updated to show it in JS. – Mark Biek Aug 18 '11 at 13:24
  • @Mark, I must admit, I like this answer the best; however, I found that some clients (ie. gmail's web interface) don't put people's name in quotes `John Doe `, so I decided to go with a slightly more robust version of @Jonathan's answer – snumpy Aug 18 '11 at 13:38
  • No worries :) It's whatever answer solves your problem most thoroughly. – Mark Biek Aug 18 '11 at 13:50
  • @snumpy, feel free to add an edit to my answer to reflect your revision. Thanks. – Jonathan M Jan 25 '12 at 18:00
3

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.

6502
  • 112,025
  • 15
  • 165
  • 265
  • 2
    My impression is that this isn't a form that's user-facing across all users. Rather it's a form that a single person is using to mass-input a bunch of data. So, while I think you make excellent points, I don't know if that's a concern here. – Mark Biek Aug 17 '11 at 20:03
  • Ma be you're right, but it's so easy for a programmer being tricked into writing obnoxious software that is just an annoyance for users. Smart software with unnecessary complex logic is sometimes a problem, especially if it's smart but not smart enough to get it right, and if there's no way to circumvent and disable this half-smartness. – 6502 Aug 18 '11 at 07:33
3
var emailList = userInput
    .replace(/[^,;]*.?</g, "")
    .replace(/>/g, "")
    .replace(/[,; ]{1,}/g, "\n")
    .replace(/[\n]{2,}/g, "\n")
    .split("\n")

This allows the email list to be provided in the following formats (including copy and paste email list from you Google To box):

"Bob Rob"<bob@company.com>, job@company.com; rob@company.com job@somewhere.com

The email Ids can be separated by ,, ;, or newlines.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
2

You can use the .math() method to quickly parse out the emails into an array:

inputval.match(/[A-z0-9]+@[A-z0-9]+.[A-z]{2,3}/g)

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.

Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
  • doesnt match Blow@example.coms.co.nr – Code Guy Apr 01 '17 at 05:09
  • @CodeGuy You are correct. I did note that the regex is very simplified. Email regex are a dime a dozen and can easily be substituted for what I have above. The point that I did not clearly convey was that `.match` will return the matches. It's an easy way to parse out the simple email addresses, which is what OP was asking for. While I encourage other people to edit my answers if they can improve them, the exact regex was not the focus of my answer. Regardless, if you want to add a better regex to improve on what I have here, feel free to do so. – Joseph Marikle Apr 03 '17 at 03:45
-2
myEmailList=userInput.match(/[a-zA-z0-9_.]+@[a-zA-Z0-9_.]+\.(com|org|whatever)/g);
myEmailListString=myEmailList.join(', ');

Or just do the first line if you're wanting an array of the email addresses.

Jonathan M
  • 17,145
  • 9
  • 58
  • 91
  • 6
    Please don’t make your own regular expressions to parse email addresses. Seriously? (com|org|whatever)? – Alan H. Jan 19 '12 at 01:20
  • 2
    Yeah, the 'whatever' was a place holder for whatever TLD's he wanted to insert. Although, with the new ruling and a few hundred thousand dollars, 'whatever' could be a TLD. :) – Jonathan M Jan 20 '12 at 19:25
  • 4
    exactly, it wasn’t that I didn’t understand "whatever", just that **I violently disagree with this approach.** – Alan H. Jan 25 '12 at 00:07
  • 3
    Furthermore, there are valid addresses that this expression does not even come close to matching. PLEASE do not do this sort of crap. – Alan H. Jan 25 '12 at 00:08
  • 4
    Chill, bro. There's plenty of room here for your answer to be added. – Jonathan M Jan 25 '12 at 05:41
  • Find the one which made many websites allow invalid address and disallow valid ones. – Gustavo Rodrigues Aug 06 '15 at 13:47