aspose file tools*
The moose likes Java in General and the fly likes java email validator jtextfield - problem with multiple emai addresses Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "java email validator jtextfield - problem with multiple emai addresses" Watch "java email validator jtextfield - problem with multiple emai addresses" New topic
Author

java email validator jtextfield - problem with multiple emai addresses

domingos manuel
Greenhorn

Joined: Sep 24, 2009
Posts: 21
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

Domingos Manuel
South Africa
skype: dm.manuel
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java email validator jtextfield - problem with multiple emai addresses