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 = "^(.+)@(.+)$";
//String regex = "^[A-Za-z0-9+_.-]+@(.+)$";
//String regex = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$";
//String regex = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+(?:\\.[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+)*@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$";
// 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);
   } catch (AddressException ex) {
      result = false;
   return result;

you can use this plugin also for email suggester:

validation in UI, here is the simple tutorials,

No comments:

Post a Comment


Most recent Elastic Search tutorials for beginner, intermediate and advance level developer, Yubraj Pokharel, in CodingWorkspace