How should I validate an e-mail address?

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

    If you are using API 8 or above, you can use the readily available Patterns class to validate email. Sample code:

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

    By chance if you are even supporting API level less than 8, then you can simply copy the Patterns.java file into your project and reference it. You can get the source code for Patterns.java from this link

提交回复
热议问题