• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

java email validator jtextfield - problem with multiple emai addresses

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 22458
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 26793
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Right! We're on it! Let's get to work tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic