I would like to enter multiple email addresses in a single text-field, separate them with a semi-colon, and still be able to validate the input.
For only one email address, I could succeed fine (see sample code below).
Can please anyone tell how do I validate multiple email addresses? And how to semi-colon delimiter them (suppose the user uses a colon)
You already have the regular expression for one single address. All you need to do is combine that to multiple, using the * operator:
I stored the valid expression for a single address into a variable so I don't have to type it twice. I made the variable final so the compiler will actually inline it.
The actual expression is then simply ^, followed by a single valid email address, followed by 0 or more occurrences of (optional whitespace, ;, optional whitespace, valid email address), followed by $.
I personally would use JavaMail and its InternetAddress class; that has parsing of its own, although it uses , for the separator.
Rob Spoor wrote:I personally would use JavaMail and its InternetAddress class; that has parsing of its own, although it uses , for the separator.
That's because the comma is the official standard separator for lists of e-mail addresses and the semicolon is a legal character in an e-mail address, according to the e-mail RFCs.
However in real life far too many people have been brainwashed by Microsoft Outlook to believe that semicolons are the right character to separate e-mail addresses. You aren't going to be able to rehabilitate all of those people. So what I do is to change all semicolons to commas before I pass the list to the Java Mail classes.
subject: java email validator jtextfield - problem with multiple emai addresses