Lindsay Presley
Lindsay Presley

Reputation: 1

Password length through Firebase authentication

How do I make an error message on password length? through Firebase Authentication for login. If final EditText mpassword.mpassword.setError("Password Must be Between 8 and 15 Characters."); return; I have tried to make an if/else statement out of the password length, but that didn't even seem to work right. Here is the code I have so far.

    package com.debatewithus.ui.login;
    
    import android.content.Intent;
    import android.os.Bundle;
    
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.text.TextUtils;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.debatewithus.MainActivity;
    import com.debatewithus.R;
    import com.debatewithus.User;
    import com.debatewithus.UserLocalStore;
    import com.google.android.gms.tasks.OnCompleteListener;
    import com.google.android.gms.tasks.Task;
    import com.google.firebase.auth.AuthResult;
    import com.google.firebase.auth.FirebaseAuth;
    
    import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
    
    public class LoginActivity extends AppCompatActivity {
    
    
        protected void onCreate(Bundle savedInstanceState, ClassNotFoundException task) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sign_up);
    
            Button login;
            Button sign_up;
            final EditText mail;
            final EditText mpassword;
            TextView forgotpassword;
            final FirebaseAuth auth;
    
            auth = FirebaseAuth.getInstance();
    
            mail = findViewById(R.id.username);
            mpassword = findViewById(R.id.Password);
            login = findViewById(R.id.login);
            sign_up = findViewById(R.id.signup);
            forgotpassword = findViewById(R.id.forgotpassword);
    
            login.setOnClickListener((new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    finish();
                }
            }));
            sign_up.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    String email_address = mail.getText().toString().trim();
                    String password = mpassword.getText().toString().trim();
    
                    if (TextUtils.isEmpty(email_address)) {
                        mail.setError("Email is Required.");
                        return;
                    }
                    if (TextUtils.isEmpty(password)) {
                        mpassword.setError("Password Required.");
                        return;
    
                        if (!(password.length() < 8 && (password.length() > 15))) {
                        final EditText mpassword.mpassword.setError("Password Must be Between 8 and 15 Characters.");
                        return;
    
                            }
                        auth.signInWithEmailAndPassword(email_address, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                                        @Override
                                        public void onComplete(@NonNull Task<AuthResult> task) {
                                            if (task.isSuccessful())
                                                startActivity(new Intent(getApplicationContext(), MainActivity.class) {
                                                });
                                            else {
                                                Toast.makeText(LoginActivity.this, "Error!" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                                                startActivity(new Intent(getApplicationContext(), MainActivity.class));
    
                                                    }
                                                };
    
                                            });
                                        };
    
                                    };
                        });
        };
            };

Upvotes: 0

Views: 206

Answers (2)

SoftwareGuy
SoftwareGuy

Reputation: 1479

Replace AND with OR , then remove ! . That should do it.

if ((password.length() < 8 || (password.length() > 15))) {

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317692

You need a logical OR with || instead of a logical AND with &&:

if (!(password.length() < 8 || (password.length() > 15)))

This will evaluate to true if the password length if less than 8 OR greater than 15.

Upvotes: 1

Related Questions