• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java email validator jtextfield - problem with multiple emai addresses

 
domingos manuel
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everyone.
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)

boolean isValidEmail = false;
String validExpression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
Pattern compare = Pattern.compile(validExpression, Pattern.CASE_INSENSITIVE);
Matcher matcher = compare.matcher(getEmailAddressTextField().getText());
if(matcher.matches()) {
isValidEmail = true;
getEmailAddressTextField().setText(getEmailAddressTextField().getText().concat(";"));
}
else
//error-code goes here

Thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic