Atif Pervaiz Atif Pervaiz -4 years ago 106
Java Question

How to validate Email & Password input from EditText before registering user using Android Studio


How to validate Email & Password input from EditText before
registering user using Android Studio...

Email: must contain @ symbol and other general requirements Password:
must >6 digits.


this is screenshot of UserRegisterActivity

Please modify this code.
My code is...

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{

private Button buttonRegister;
private EditText editTextEmail;
private EditText editTextPassword;
private TextView textViewSignin;

private ProgressDialog progressDialog;

private FirebaseAuth firebaseAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);

firebaseAuth=FirebaseAuth.getInstance();
if (firebaseAuth.getCurrentUser()!=null){
//profile activity here
finish();
startActivity(new Intent(getApplicationContext(),ProfileActivity.class));
}

progressDialog = new ProgressDialog(this);

buttonRegister = (Button)findViewById(R.id.buttonRegister);
editTextEmail = (EditText)findViewById(R.id.editTextEmail);
editTextPassword = (EditText)findViewById(R.id.editTextPassword);
textViewSignin = (TextView)findViewById(R.id.textViewSignin);

buttonRegister.setOnClickListener(this);
textViewSignin.setOnClickListener(this);
}

private void registerUser(){
final String email = editTextEmail.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();

//uset cannot go any further without entering password
if(TextUtils.isEmpty(email)){
//email is empty
Toast.makeText(this, "Please enter email",Toast.LENGTH_SHORT).show();
//stopping execution further
return;
}
if(TextUtils.isEmpty(password)){
//password is empty
Toast.makeText(this, "Please enter password",Toast.LENGTH_SHORT).show();
//stopping execution further
return;
}

//if validations are ok
//we will first show progressbar
progressDialog.setMessage("Registerating User...");
progressDialog.show();

firebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {

if(task.isSuccessful()){
//user is successfully registered. we will start profile activity here
FirebaseUser user = firebaseAuth.getCurrentUser();
Toast.makeText(RegisterActivity.this, "Authentication success. " + user.getUid(), Toast.LENGTH_SHORT).show();
progressDialog.hide();
finish();
startActivity(new Intent(getApplicationContext(),ProfileActivity.class));
}
else{
Toast.makeText(RegisterActivity.this, "Could not register. please try again", Toast.LENGTH_SHORT).show();
progressDialog.hide();
}

}
});
}

@Override
public void onClick(View v) {
if (v==buttonRegister){
registerUser();
}
if(v==textViewSignin){
finish();
startActivity(new Intent(this, LoginActivity.class));
}
}


}

Answer Source

try this my friend

 String emailAddress = etSignInEmail.getText().toString().trim();
    if (etSignInPassword.getText().toString().length() < 6) {
    etSignInPassword.setError(getString("password minimum contain 6 character));
    etSignInPassword.requestFocus();
    }
    if (etSignInPassword.getText().toString().equals("")) {
    etSignInPassword.setError(getString("please enter password));
    etSignInPassword.requestFocus();
    }
    if (!android.util.Patterns.EMAIL_ADDRESS.matcher(emailAddress).matches()) {
    etSignInEmail.setError(getString("please enter valid email address));
    etSignInEmail.requestFocus();
    }
    if (etSignInEmail.getText().toString().equals("")) {
    etSignInEmail.setError(getString("please enter email address));
    etSignInEmail.requestFocus();
    }
    if (!emailAddress.equals("") &&
    etSignInPassword.getText().toString().length() >= 6 &&
    !etSignInPassword.getText().toString().trim().equals("")
    && android.util.Patterns.EMAIL_ADDRESS.matcher(emailAddress).matches()) {
    // do  your action
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download