如何在android中验证多个逗号分隔的电子邮件ID?

时间:2013-05-23 12:21:14

标签: java android

如何验证android中的多个逗号(,)分隔的电子邮件ID。我使用下面的代码进行单个电子邮件验证。请任何人帮助我。

static boolean validEmail(String str_newEmail) {


         return str_newEmail.matches("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
        }

提前感谢。

3 个答案:

答案 0 :(得分:5)

您好使用此代码我认为这将有助于您

String str_newEmail = "Bharath@in.com,bharath@appsforbb.co.in";
    String[] test = str_newEmail.split(",");
    for (int i = 0; i < test.length; i++) {
        if (!validEmail(test[i])) {
            System.out.println("This is not a vaild email");
            break;
        }
    }

验证电子邮件方法。

    public boolean validEmail(String str_newEmail) {

    return str_newEmail.matches("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
}

答案 1 :(得分:1)

private boolean isValidEmail(CharSequence target) {
    boolean emailflag = false;
    String emailArr[] = target.toString().split("[,]");
    for (int i = 0; i < emailArr.length; i++) {
        emailflag = android.util.Patterns.EMAIL_ADDRESS.matcher(
                emailArr[i].trim()).matches();
    }
    return emailflag;
}

答案 2 :(得分:0)

您也可以使用StringTokenizer:

String emails = "Bharath@in.com,bharath@appsforbb.co.in";
StringTokenizer st = new StringTokenizer(emails, ",");

while (st.hasMoreElements()) {
  String email = st.nextToken();
  if (!validEmail(email)) {
      System.out.println("Invalid email:" + email);
  }
}