0

In my project I give email address to send the mail in a text box. I can give either a single email address or a multiple email address separated by commas. Is there any regular expression for this situation. I used the following code and its check for single email address only.

public static boolean isEmailValid(String email){  

            boolean isValid = false;  
        String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";  
        CharSequence inputStr = email;  

        Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);  
        Matcher matcher = pattern.matcher(inputStr);  
        if(matcher.matches()){  
        isValid = true;  
        }  
        return isValid;  
        } 
Manikandan
  • 1,479
  • 6
  • 48
  • 89
  • 3
    I may be daft, but just split by comma and parse each token? Given a comma isn't a valid email character, you should only be able to have valid values between. (You may also want to trim the tokens before parsing to avoid white space failures). – Brad Christie Sep 22 '11 at 14:26
  • 1
    It should also be noted that [regex is not a very good way to validate an email address](http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses/201378#201378). – unholysampler Sep 22 '11 at 14:30
  • 1
    I hope you are aware that your regex is very simple and will miss a lot of valid email addresses, because you don't allow valid characters to be part of the email address. – stema Sep 22 '11 at 14:42
  • In addition to what **stema** and **unholysampler** said, this regex will block top-level domains longer than 4 characters, which do exist. [.museum](http://www.smithsonian.museum/) and [.travel](http://www.travel.travel/), for example, or [internationalized country-code TLDs](http://en.wikipedia.org/wiki/Internationalized_country_code_top-level_domain) such as [.δοκιμή](http://παράδειγμα.δοκιμή). – Justin Morgan - On strike Sep 22 '11 at 15:51

2 Answers2

1
  1. Split the input by a delimiter in your case ','.
  2. Check each email if its a valid format.
  3. Show appropriate message (email 1 is valid , email 2 is not valid , email 3 is not valid etc etc)
David
  • 4,185
  • 2
  • 27
  • 46
0

You can split your email-var on a ",", and check for each emailaddress you got :)

Snicksie
  • 1,987
  • 17
  • 27