0

I'm looking to set a validation condition for email whereby after ".xxx" (email termination, e.g: john123@gmail.xxx) the char limit is less than 3 but more than 2 only (e.g: invalid if john123@gmail.c or @gmail.commm).

here is my attempt:

public final boolean validEmail(String target){

        boolean valid_dot_com

        if(target.toString().contains(".")){

           int indexDot = target.toString().indexOf(".");

           //  substring from char containing "." to last char
           String temp = target.toString().substring(indexDot,         
           target.length());

           if(temp.length()<2 && temp.length()>3){
              valid_dot_com = false;
            }
        }    

   return valid_dot_com && Patterns.EMAIL_ADDRESS.matcher(target).matches();   
}

However, this code does not return the result that I needed.

I do have the theory that the Patterns.EMAIL_ADDRESS overwrite my boolean value causing the condition checking to become true even when its not.

Do enlighten me!

Edit:

I've found my answer!

through an online regex generator: https://regex101.com/ I have been able to generate a custom regex pattern to compile and do my validation. Rest of the code is similar to just simple conditions.

Thanks all for the reply!

Positive-One
  • 89
  • 1
  • 12

10 Answers10

1

You can use inbuilt fucntion.

if (!Patterns.EMAIL_ADDRESS.matcher(et_email.getText().toString()).matches()) 

{ your code. }

1

Use this:

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

Please refer link

Community
  • 1
  • 1
fightingCoder
  • 594
  • 3
  • 7
1

try this custom pattern

String EMAIL_PATTERN = "^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-z0-9])?\\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$";

public final boolean validateEmail(String target) {
    if (target !=null && target.length() > 1) {
        Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(target);
        return matcher.matches();
    } else if (target.length() == 0) {
        return false;
    } else {
        return false;
    }
}
Mehul Gajjar
  • 431
  • 5
  • 20
  • Hi, could you explain the email pattern to me? I'm confuse as how it implements. how does it actually differentiate between ".com" with ".c" or ".commm"? – Positive-One Apr 11 '17 at 07:21
  • for brief information see https://developer.android.com/reference/java/util/regex/Pattern.html – Mehul Gajjar Apr 11 '17 at 10:34
1

try this one validate method

  public static final Pattern VALID_EMAIL_ADDRESS_REGEX =
        Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);

public static boolean validate(String emailStr) {
    Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(emailStr);
    return matcher.find();
}
Nikhil Sharma
  • 593
  • 7
  • 23
1

Use this

!TextUtils.isEmpty(email) && Patterns.EMAIL_ADDRESS.matcher(email).matches()
Aldrin Joe Mathew
  • 472
  • 1
  • 4
  • 13
1

pass your email string to validate to this function below, and it will return boolean either it is valid or not

public static boolean isEmailValid(String email) {
    boolean isValid = false;

    String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
    CharSequence inputStr = email;

    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    if (matcher.matches()) {
        isValid = true;
    }
    return isValid;
}
0

The following will validate email.

public static boolean validateEmail(String email) {

        Pattern emailPattern = Pattern.compile(".+@.+\\.[a-z]+");
        Matcher emailMatcher = emailPattern.matcher(email);
        return emailMatcher.matches();

    }
Fathima km
  • 2,539
  • 3
  • 18
  • 26
0

This splits the email string using dot as a delimiter, and gets 2nd string value (at index 1 as it starts from 0) and compares it to check if its 3 or less and greater than 1 after dot.

String st = email.split("\\.")[1];
if(st.length() <=3 && st.length() > 1) {
   // its 3 or less but greater than 1 after dot .
}
Sharoz Khalid
  • 121
  • 1
  • 9
  • While this code may answer the question, providing additional [context](https://meta.stackexchange.com/q/114762) regarding _how_ and/or _why_ it solves the problem would improve the answer's long-term value. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit](http://stackoverflow.com/posts/43338770/edit) your answer to add an explanation, and give an indication of what limitations and assumptions that apply. It also doesn't hurt to mention why this answer is more appropriate than others. – Dev-iL Apr 11 '17 at 07:33
0

Try This One

public final boolean validEmail(String target) {
        Log.d("RESULT", "----------------------------------Receieved: " + target);
        Log.d("RESULT", "----------------------------------String Length: " + target.length());
        Log.d("RESULT", "----------------------------------Index of Dot: " + target.toString().indexOf("."));

        boolean valid_dot_com = true;

        if (target.toString().contains(".")) {

            int indexDot = target.toString().indexOf(".");

            //  substring from char containing "." to last char
            String temp = target.toString().substring(indexDot + 1,
                    target.length());
            Log.d("RESULT", "----------------------------------Sub String : " + temp);
            Log.d("RESULT", "----------------------------------Sub String Lenght  : " + temp.length());
            if ((temp.length() > 3)) {
                valid_dot_com = false;
            }
        }

        return valid_dot_com && Patterns.EMAIL_ADDRESS.matcher(target).matches();
    }
RAJESH KUMAR ARUMUGAM
  • 1,560
  • 21
  • 35
0

Already Answered Here

public final static boolean isValidEmail(CharSequence target) {
return !TextUtils.isEmpty(target) && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
Community
  • 1
  • 1
Amit Sharma
  • 645
  • 5
  • 13