How to validate email address in Android or Android Studio

Email address validation simple the check the given address match of pattern or not. Here is the simple example to validate email,

public final static Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile(
            "[a-zA-Z0-9+._%-+]{1,256}" +
            "@" +
            "[a-zA-Z0-9][a-zA-Z0-9-]{0,64}" +
            "(" +
            "." +
            "[a-zA-Z0-9][a-zA-Z0-9-]{0,25}" +
            ")+"
        );

public static boolean checkEmail(String email) {
        return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
    }

Here checkEmail Address take parameter email and it return true or false, if email address valid then true otherwise it return false boolean value.

----------------------------------------------------------------------------

There are another method, Java Regex : Validate Email Address

String regex = "^(.+)@(.+)$";
//or
//String regex = "^[A-Za-z0-9+_.-]+@(.+)$";
//or
//String regex = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$";
//or
//String regex = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+(?:\\.[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+)*@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$";
//or
// personally, i recommend this.
//String regex = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$";
Pattern pattern = Pattern.compile(regex);

public static boolean checkEmail(String email) {
         Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }

---------------------------------------------------------------------------
You can use this ,

public static boolean isValidEmailAddress(String email) {
   boolean result = true;
   try {
      InternetAddress emailAddr = new InternetAddress(email);
      emailAddr.validate();
   } catch (AddressException ex) {
      result = false;
   }
   return result;
}


you can use this plugin also for email suggester: https://github.com/Fewlaps/quitnow-email-suggester

validation in UI, here is the simple tutorials, https://android-arsenal.com/details/1/1605
How to validate email address in Android or Android Studio How to validate email address in Android or Android Studio     Reviewed by D Kshetri on 11:48 AM Rating: 5

No comments:

Powered by Blogger.