Manikandan
Manikandan

Reputation: 1519

Regular expression which accepts multiple email addresses seperated by comma in java

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;  
        } 

Upvotes: 0

Views: 2072

Answers (2)

David
David

Reputation: 4285

  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)

Upvotes: 1

Snicksie
Snicksie

Reputation: 1997

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

Upvotes: 0

Related Questions