Reputation: 527
I'm having some problem on validation. How to change my coding so that the validation can come one at a time. I'm watching to many tutorial and here the result. I want my validation came one by one. So how it should be? Thanks in advance.
private void RegisterAccount(String firstname, String lastname, String email, String password, String confirmpass)
{
if (TextUtils.isEmpty(firstname))
{
Toast.makeText(Signup.this, "Enter your first name.", Toast.LENGTH_LONG).show();
}
if (TextUtils.isEmpty(lastname))
{
Toast.makeText(Signup.this, "Enter your last name.", Toast.LENGTH_LONG).show();
}
if (TextUtils.isEmpty(email))
{
Toast.makeText(Signup.this, "Enter your valid email address.", Toast.LENGTH_LONG).show();
}
if (TextUtils.isEmpty(password))
{
Toast.makeText(Signup.this, "Enter your password.", Toast.LENGTH_LONG).show();
}
if (TextUtils.isEmpty(confirmpass)) {
Toast.makeText(Signup.this, "Please write your password again.", Toast.LENGTH_LONG).show();
}
if (password != confirmpass)
{
Toast.makeText(Signup.this, "Password do not match", Toast.LENGTH_LONG).show();
}
else
{
loadingBar.setTitle("Creating New Account");
loadingBar.setMessage("Please wait while we are creating account for you.");
loadingBar.show();
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>()
{
@Override
public void onComplete(@NonNull Task<AuthResult> task)
{
if(task.isSuccessful())
{
Toast.makeText(Signup.this, "You have successfully signed up", Toast.LENGTH_SHORT).show();
Intent mainIntent = new Intent(Signup.this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mainIntent);
finish();
}
else
{
Toast.makeText(Signup.this, "Error occured, please try again.", Toast.LENGTH_SHORT).show();
}
loadingBar.dismiss();
}
});
}
}
}
Upvotes: 0
Views: 59
Reputation: 74
You should use else -if instead of if.
if (TextUtils.isEmpty(firstname))
{
Toast.makeText(Signup.this, "Enter your first name.", Toast.LENGTH_LONG).show();
} else if (TextUtils.isEmpty(lastname))
{
Toast.makeText(Signup.this, "Enter your last name.", Toast.LENGTH_LONG).show();
} else if (TextUtils.isEmpty(email))
{
Toast.makeText(Signup.this, "Enter your valid email address.", Toast.LENGTH_LONG).show();
}else if (TextUtils.isEmpty(password))
{
Toast.makeText(Signup.this, "Enter your password.", Toast.LENGTH_LONG).show();
} else if (TextUtils.isEmpty(confirmpass)) {
Toast.makeText(Signup.this, "Please write your password again.", Toast.LENGTH_LONG).show();
}else if (password != confirmpass)
{
Toast.makeText(Signup.this, "Password do not match", Toast.LENGTH_LONG).show();
}
Upvotes: 1
Reputation: 111
private boolean validateName() {
if (inputName_reg.getText().toString().trim().isEmpty() ) {
inputLayoutName_reg.setError(getString(R.string.err_msg_name));
requestFocus(inputName_reg);
return false;
} else{
inputLayoutName_reg.setErrorEnabled(false);
}
return true;
}
You need to use "else If" and not just "if" that way the code works faster. use booleans to validate, they are more dynamic. I hope I have helped you, Regards !!
Upvotes: 0