How should I validate an e-mail address?

后端 未结 30 1639
臣服心动
臣服心动 2020-11-22 08:25

What\'s a good technique for validating an e-mail address (e.g. from a user input field) in Android? org.apache.commons.validator.routines.EmailValidator doesn\'t seem to be

30条回答
  •  抹茶落季
    2020-11-22 09:13

    Another option is the built in Patterns starting with API Level 8:

    public final static boolean isValidEmail(CharSequence target) {
      if (TextUtils.isEmpty(target)) {
        return false;
      } else {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
      }
    }
    

    Patterns viewable source

    OR

    One line solution from @AdamvandenHoven:

    public final static boolean isValidEmail(CharSequence target) {
      return !TextUtils.isEmpty(target) && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
    }
    

提交回复
热议问题