How should I validate an e-mail address?

后端 未结 30 2025
臣服心动
臣服心动 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:09

    You could also use

    InternetAddress emailAddr = new InternetAddress(email);
    emailAddr.validate();
    

    If the email is not valid it will throw an AddressException.

    Unfortunately Android doesn't support jndi-dns, but just to give you an idea of a more powerful email validation, you could use it to validate the email domain. Maybe an Android guru could help and show if there are similar alternatives... An example implementation with "regular" java is available here.

    EDIT

    I just realized that javax.mail isn't support neither... But this post shows a workaround.

提交回复
热议问题