-3

So I have this method which returns true if the validation is correct:

private boolean validation() {
    String emailStr = etEmail.getText().toString();
    if (emailStr.trim().isEmpty()) {
        etEmail.setError(getResources().getString(R.string.eroareEmpty));
        return false;
    } else if (!emailStr.endsWith("stud.ase.ro") && emailStr.length() <= 15) {
        etEmail.setError(getResources().getString(R.string.eroareEmail));
        return false;
    }

    return true;
}

I want to verify that the text i type in EditText etEmail contains (only at the end of the string) "stud.ase.ro", the whole string not just a part of it. In simple words, i want to verify if the email ends with "stud.ase.ro" and nothing else.

Currently my method returns true, even if i type something unsual like "hellllllllllllooo@stud", which it shouldn't do.

Miki
  • 1
  • 3
  • Welcome to SO. First of all, introduce local variables for expressions like `etEmail.getText()` so code becomes more readable. Second of all, remove stuff that is not relevant to the question - like `etPassword.setError()`. Third of all, consider browsing API doc on `String` class before asking. Cheers – no id May 04 '18 at 00:02
  • 4
    Possible duplicate of [How to check if a String contains another String in a case insensitive manner in Java?](https://stackoverflow.com/questions/86780/how-to-check-if-a-string-contains-another-string-in-a-case-insensitive-manner-in) – yassadi May 10 '18 at 03:19

1 Answers1

1

To match at the end of the string, use the String.endsWith method.

if (str.endWiths("hello123@stud")) {

  //bla bla
}

You might want to improve your code. You're using EditText right?

  1. EditText.getText() does not return null
  2. You should create a local variable for repeated code access the text inside EditText: String emailStr = etEmail.getText().toString()

It will increase readability a lot.

Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51
  • @Manh it doesn't work. i can still add something abnormally like "hellooooooooooo@" and it works. – Miki May 04 '18 at 00:04
  • "hellooooooooooo@" does not ends with "hellllllllllllooo@stud" so I am pretty sure my suggested method return `false` – Mạnh Quyết Nguyễn May 04 '18 at 00:06
  • If there an error inside your method, you should check the second condition `!emailStr.endsWith(STUDENT_ID) && emailStr.length() <= 15` – Mạnh Quyết Nguyễn May 04 '18 at 00:09
  • first of all, it should and with "@stud.ase.ro", second my problem is that the method doesn't verify all the string that i'm searching. It works perfectly with half of the string, and i don't want that. I want the whole string to be verified – Miki May 04 '18 at 00:11
  • The problem is you combine the test for `emailStr.length() <= 15`. If you input `"hellooooooooooo@"` (length 16) then it will pass the second `if` condition, therefore it return true. You should have a separate `if` condition for length check – Mạnh Quyết Nguyễn May 04 '18 at 00:18
  • May I ask what's your constraint on length? Is `length() <= 15` is valid or invalid? I then might help you to combine this conditions – Mạnh Quyết Nguyễn May 04 '18 at 00:21
  • if it's less than 15 should be invalid. – Miki May 04 '18 at 00:22
  • Then it should be `emailStr.length() <= 15 || !emailStr.endsWith(STUDENT_ID)` You should use the `OR` operator here. Note that the check for length should be done first, it will gain you some performance if the input length <= 15 then no need to check for `endWiths` – Mạnh Quyết Nguyễn May 04 '18 at 00:23
  • yes, you are right. Thanks a lot for your time and patience. – Miki May 04 '18 at 00:37