title: Android volley login registration with password confirmation
body: <p>I'm currently found a problem in my android registration process, the validation seems not working fine. What i mean is that, when there is a blank field, and click register, it straight registered on my database with blank field. I'm not sure what causes it, i hope someone can help me. Below is my android java + php code</p>
  
  <p><strong>Register.java</strong></p>
  
  <pre><code>public class RegisterActivity extends AppCompatActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_register);
  
  
      final EditText etUsername = (EditText) findViewById(R.id.etUsername);
      final EditText etPassword = (EditText) findViewById(R.id.etPassword);
      final EditText etPassword2 = (EditText) findViewById(R.id.etPassword2);
      final Button bNext = (Button) findViewById(R.id.bNext);
      final Button bBLogin = (Button) findViewById(R.id.bBLogin);
  
      bNext.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
  
              final String Username = etUsername.getText().toString().trim();
              final String Acc_Pass = etPassword.getText().toString().trim();
              final String Acc_Pass2 = etPassword2.getText().toString().trim();
  
              Response.Listener&lt;String&gt; responeListener = new Response.Listener&lt;String&gt;() {
                  @Override
                  public void onResponse(String response) {
                      try {
                          JSONObject jsonRespone = new JSONObject(response);
                          boolean success = jsonRespone.getBoolean("success");
  
                          if (TextUtils.isEmpty(Username) || Username.length() &lt; 6){
                              if (TextUtils.isEmpty(Username))
                              {
                                  etUsername.setError("You are required to enter a username");
                              }
  
                              else if (Username.length() &lt; 6)
                              {
                                  etUsername.setError("Your username is too short, minimum character is 6. Please re-enter");
                              }
  
                              return;
  
                          } if (TextUtils.isEmpty(Acc_Pass) || Acc_Pass.length() &lt; 6){
                              if (TextUtils.isEmpty(Acc_Pass))
                              {
                                  etPassword.setError("Your password is empty. Please try again");
                              }
  
                              else if (Acc_Pass.length() &lt; 6)
                              {
                                  etPassword.setError("Your password is too short, minimum character is 6. Please re-enter");
                              }
  
                              return;
  
                          } if (TextUtils.isEmpty(Acc_Pass2) || !Acc_Pass2.equals(Acc_Pass)){
                              if (TextUtils.isEmpty(Acc_Pass2))
                              {
                                  etPassword2.setError("Your password is empty. Please try again");
                              }
  
                              else if (!Acc_Pass2.equals(Acc_Pass))
                              {
                                  etPassword2.setError("Password mismatch. Please try again");
                              }
  
                              return;
  
                          } else if (!success){
  
                              AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                              builder.setMessage("Register Failed Username Unavailable")
                                      .setNegativeButton("Retry", null)
                                      .create()
                                      .show();
                              return;
  
                          } else {
  
                              Intent intent = new Intent(RegisterActivity.this, RegisterActivity2.class);
                              RegisterActivity.this.startActivity(intent);
                              return;
                          }
  
                      } catch (JSONException e) {
                          e.printStackTrace();
                      }
                  }
              };
  
              RegisterRequest registerRequest = new RegisterRequest(Username, Acc_Pass, Acc_Pass2, responeListener);
              RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
              queue.add(registerRequest);
  
          }
      });
  
      bBLogin.setOnClickListener(new View.OnClickListener(){
  
          @Override
          public void onClick(View v) {
              Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
              RegisterActivity.this.startActivity(intent);
          }
      });
  
  }
  }
  </code></pre>
  
  <p><strong>RegisterRequest.java</strong></p>
  
  <pre><code>public class RegisterRequest extends StringRequest {
  
  private static final String REGISTER_REQUEST_URL = "http://192.168.1.2/AppRegister.php";
  private Map&lt;String, String&gt; params;
  
  public RegisterRequest(String Username, String Acc_Pass, String Acc_Pass2, Response.Listener&lt;String&gt; listener){
      super (Method.POST, REGISTER_REQUEST_URL, listener, null);
      params = new HashMap&lt;&gt;();
      params.put("Username", Username);
      params.put("Acc_Pass", Acc_Pass);
      params.put("Acc_Pass2", Acc_Pass2);
  }
  
  @Override
  public Map&lt;String, String&gt; getParams() {
      return params;
  }
  }
  </code></pre>
  
  <p><strong>AppRegister.php</strong></p>
  
  <pre><code>&lt;?php
  
  $connect = mysqli_connect("localhost", "root", "", "registering");
  
  $Username = $_POST["Username"];
  $Acc_Pass = $_POST["Acc_Pass"];
  $Acc_Pass2 = $_POST["Acc_Pass2"];
  
  
   function registerUser() {
      global $connect, $Username, $Acc_Pass, $Acc_Pass2;
      $statement = mysqli_prepare($connect, "INSERT INTO account_details (Username, Acc_Pass, Acc_Pass2) VALUES (?, ?, ?)");
      mysqli_stmt_bind_param($statement, "sss", $Username, $Acc_Pass, $Acc_Pass2);
      mysqli_stmt_execute($statement);
      mysqli_stmt_close($statement);     
  }
  
  
  
  function usernameAvailable() {
      global $connect, $Username;
      $statement = mysqli_prepare($connect, "SELECT * FROM account_details WHERE Username = ?"); 
      mysqli_stmt_bind_param($statement, "s", $Username);
      mysqli_stmt_execute($statement);
      mysqli_stmt_store_result($statement);
      $count = mysqli_stmt_num_rows($statement);
      mysqli_stmt_close($statement); 
      if ($count &lt; 1){
          return true; 
      }else {
          return false; 
      }
  }
  
  $response = array();
  $response["success"] = false;  
  
  if (usernameAvailable()){
      registerUser();
      $response["success"] = true;  
  }
  
  echo json_encode($response);
  mysqli_close($con);
  ?&gt;
  </code></pre>
  
  <p><strong>Updated Register.java</strong></p>
  
  <pre><code>public class RegisterActivity extends AppCompatActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_register);
  
      final EditText etUsername = (EditText) findViewById(R.id.etUsername);
      final EditText etPassword = (EditText) findViewById(R.id.etPassword);
      final EditText etPassword2 = (EditText) findViewById(R.id.etPassword2);
      final Button bNext = (Button) findViewById(R.id.bNext);
      final Button bBLogin = (Button) findViewById(R.id.bBLogin);
  
      bNext.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              final String Username = etUsername.getText().toString().trim();
              final String Acc_Pass = etPassword.getText().toString().trim();
              final String Acc_Pass2 = etPassword2.getText().toString().trim();
  
              if (etUsername.getText().toString().matches("") || etPassword.getText().toString().matches("") || etPassword2.getText().toString().matches("")) {
                  if (TextUtils.isEmpty(Username) || Username.length() &lt; 6) {
                      if (TextUtils.isEmpty(Username))
                      {
                          etUsername.setError("You are required to enter a username");
                      }
  
                      else if (Username.length() &lt; 6)
                      {
                          etUsername.setError("Your username is too short, minimum character is 6. Please re-enter");
                      }
  
                  } if (TextUtils.isEmpty(Acc_Pass) || Acc_Pass.length() &lt; 6) {
                      if (TextUtils.isEmpty(Acc_Pass))
                      {
                          etPassword.setError("Your password is empty. Please try again");
                      }
  
                      else if (Acc_Pass.length() &lt; 6)
                      {
                          etPassword.setError("Your password is too short, minimum character is 6. Please re-enter");
                      }
  
                  } if (TextUtils.isEmpty(Acc_Pass2) || !Acc_Pass2.equals(Acc_Pass)) {
                      if (TextUtils.isEmpty(Acc_Pass2))
                      {
                          etPassword2.setError("Your password is empty. Please try again");
                      }
  
                      else if (!Acc_Pass2.equals(Acc_Pass))
                      {
                          etPassword2.setError("Password mismatch. Please try again");
                      }
                  }
  
              } else {
  
                  Response.Listener&lt;String&gt; responeListener = new Response.Listener&lt;String&gt;() {
                      @Override
                      public void onResponse(String response) {
                          try {
                              JSONObject jsonRespone = new JSONObject(response);
                              boolean success = jsonRespone.getBoolean("success");
  
                              Intent intent = new Intent(RegisterActivity.this, RegisterActivity2.class);
                              RegisterActivity.this.startActivity(intent);
  
                          } catch (JSONException e) {
                              e.printStackTrace();
                          }
                      }
                  };
  
                  RegisterRequest registerRequest = new RegisterRequest(Username, Acc_Pass, Acc_Pass2, responeListener);
                  RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
                  queue.add(registerRequest);
  
              }
          }
      });
  
      bBLogin.setOnClickListener(new View.OnClickListener(){
  
          @Override
          public void onClick(View v) {
              Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
              RegisterActivity.this.startActivity(intent);
          }
      });
  
  }
  }
  </code></pre>