ANDROID IF/ELSE FAILS CONTINUES TO EXECUTE JSON
- by Keith Cesar Haizlett
I am trying to create a Registration app with JSON to connect and post to MYSQL database. I created the following IF/ELSE statements to check for vacant input boxes, password match, and correct email characters before allowing it to be entered into the DATABASE. The code continues to execute the JSON posting even after the passwords don't match , invalid email characters are entered , and vacant text boxes are submitted. Why is it not returning and continuing to execute the JSON code?
try {
if (!inputEmail.getText().toString().matches("[a-zA-Z0-9._-]+@[a-z]+.[a-z]+") && email.length() > 0)
{
Toast.makeText(getApplicationContext(), "Enter Valid Email Address", Toast.LENGTH_LONG).show();
return;
}
else if(name.equals("") || email.equals("")|| password.equals("")||check.equals(""))
{
Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
return;
}
// check if both password matches
else if(!password.equals(checkpass))
{
Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
return;
}
if (json.getString(KEY_SUCCESS) != null) {
registerErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully registred
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Registration Screen
finish();
}else{
// Error in registration
registerErrorMsg.setText("User already Registered");
}
}
}
catch (JSONException e) {
}
}
});