1

I want a method to login to my Parse account with either my Username OR my Email. The code I have now works only with my username. I want to know a way to identify if the text field has a username or an email.

Here is the code,

private class SignInOnClickListener implements View.OnClickListener {

    @Override
    public void onClick(View v) {
        // Get the username and password from the view
        final String username_email = mUsernameEmailEtxt.getText().toString().toLowerCase();
        final String password = mPasswordEtxt.getText().toString();

        if (isFormInputValid(username_email, password)) {
            if (android.util.Patterns.EMAIL_ADDRESS.matcher(username_email).matches()) { // HERE!
                final String email = mUsernameEmailEtxt.getText().toString();
                ParseUser.logInInBackground(email, password, new LogInCallback() {
                    public void done(ParseUser user, ParseException e) {
                        if (user != null) {
                            // Hooray! The user is logged in.
                            Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();
                        }
                    }
                });
            } else {
                ParseUser.logInInBackground(username_email, password, new LogInCallback() {

                    public void done(ParseUser user, ParseException e) {
                        if (user != null) {
                            // Hooray! The user is logged in.
                            Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();
                        } else {
                            findViewById(R.id.error).setVisibility(View.VISIBLE);
                        }
                    }
                });
            }
        }
    }
}

private class SignInOnClickListener implements View.OnClickListener {

    @Override
    public void onClick(View v) {
        // Get the username and password from the view
        final String username_email = mUsernameEmailEtxt.getText().toString().toLowerCase();
        final String password = mPasswordEtxt.getText().toString();

        if (isFormInputValid(username_email, password)) {
            if (android.util.Patterns.EMAIL_ADDRESS.matcher(username_email).matches()) { // HERE!
                final String email = mUsernameEmailEtxt.getText().toString();
                ParseUser.logInInBackground(email, password, new LogInCallback() {
                    public void done(ParseUser user, ParseException e) {
                        if (user != null) {
                            // Hooray! The user is logged in.
                            Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();
                        }
                    }
                });
            } else {
                ParseUser.logInInBackground(username_email, password, new LogInCallback() {

                    public void done(ParseUser user, ParseException e) {
                        if (user != null) {
                            // Hooray! The user is logged in.
                            Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();
                        } else {
                            findViewById(R.id.error).setVisibility(View.VISIBLE);
                        }
                    }
                });
            }
        }
    }
}

UPDATE: I've put up logs and made some changes (credits: @kevin), detects emails but refuses to login with it.

Here is the code,

 private class SignInOnClickListener implements View.OnClickListener {

    @Override
    public void onClick(View v) {
        // Get the username and password from the view
        final String username_email = mUsernameEmailEtxt.getText().toString().toLowerCase();
        final String password = mPasswordEtxt.getText().toString();
        final String email = mUsernameEmailEtxt.getText().toString().toLowerCase();

        if (isFormInputValid(username_email, password)) {
            if (username_email.indexOf('@') != -1) { // HERE!
                Log.d("detector", "username_email detected as email:" + email.toString());
                ParseUser.logInInBackground(email, password, new LogInCallback() {
                    public void done(ParseUser user, ParseException e) {
                        if (user != null) {
                            // Hooray! The user is logged in.
                            Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();
                        }
                    }
                });
            } else {
                Log.d("detector", "username_email detected as username:" + username_email.toString());
                ParseUser.logInInBackground(username_email, password, new LogInCallback() {
                    public void done(ParseUser user, ParseException e) {
                        if (user != null) {
                            // Hooray! The user is logged in.
                            Intent intent = new Intent(getBaseContext(), MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();
                        } else {
                            findViewById(R.id.error).setVisibility(View.VISIBLE);
                            Log.d("error", "username or email invalid");
                        }

                    }
                });
            }

        }
    }
}
Chehanr
  • 81
  • 9

4 Answers4

2

This is not difficult to do and you should use in-built patterns already provided in Android to check email ids perfectly.

public final static boolean isEmailIDValid(CharSequence email) {
    if (email == null) 
        return false;

    return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}

Less complicated and works perfectly on all Android versions above API 8. I have been using this in any app supporting API 14 and above, and never had any problems.

Aritra Roy
  • 15,355
  • 10
  • 73
  • 107
  • That's the condition he was originally using. I'm starting to suspect the problem isn't with the condition. – Kevin Krumwiede Aug 13 '15 at 07:18
  • @AritraRoy from where you copied? `android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();` did you written yourself? – Rustam Aug 13 '15 at 07:22
  • @Rustam Yes, I am using it in my app for a long time. But if you don't intend to believe, I would not try to. You can think what you want to. Thanks. – Aritra Roy Aug 13 '15 at 07:33
1

It's extremely complicated to validate an email address. That article suggests that you simply try sending an email to the address, which doesn't exactly fit your use case, but think along those lines. Maybe do a crude validation, like testing whether the field contains @, and if it does, try to use it as an email.

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82
  • I've tried it. Still refuses to identify as an email. ' if (!username_email.matches("@")) { // HERE!' – Chehanr Aug 13 '15 at 07:10
  • Try `if(username_email.indexOf('@') != -1) ...` – Kevin Krumwiede Aug 13 '15 at 07:12
  • @Chehanr There aren't very many ways that test could fail. Are you sure it's the test that's failing? Have you added logging statements to determine which code is being executed? – Kevin Krumwiede Aug 13 '15 at 07:19
  • Well, it does detect when the text field contains an Email! But still refuses to login to my parse account. ` 08-13 12:54:07.102 17634-17634/com.chehanr.sample D/detector﹕ username_email detected as email` – Chehanr Aug 13 '15 at 07:25
0

that's not that complicated you can try this, it give you true if email contains only low capilat letters, @ and ".".

David
  • 2,331
  • 4
  • 29
  • 42
0

Use Apache Validation Library . Its small, concise and does the Job.

userEditText.addTextChangedListener(new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        textView.setVisibility(View.VISIBLE);
    }

    public void afterTextChanged(Editable s) {
        EmailValidator emailValidator = EmailValidator.getInstance();
        if (!emailValidator.isValid(s.toString())) {
          //Is Email
        } else {
         //Is username   
        }
    }
});

Note: Hope you have accounted for edge cases as your username could also be an email judging by your requirements

humblerookie
  • 4,717
  • 4
  • 25
  • 40