DT @17.11.2015: Added input validation functions to login/register screens
This commit is contained in:
@@ -10,6 +10,7 @@ import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.util.Log;
|
||||
import android.util.Patterns;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
@@ -23,6 +24,7 @@ import org.deke.risk.riskahead.fragments.TwitterButtonFragment;
|
||||
import org.deke.risk.riskahead.helper.AppConfig;
|
||||
import org.deke.risk.riskahead.helper.AppController;
|
||||
import org.deke.risk.riskahead.helper.SessionManager;
|
||||
import org.deke.risk.riskahead.helper.TextValidator;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
@@ -40,10 +42,13 @@ public class LoginActivity extends AppCompatActivity implements FacebookButtonFr
|
||||
private final static String EXTRA_MESSAGE = "org.deke.risk.riskahead.MESSAGE";
|
||||
private final static String mActivityTitle = "RiskAhead";
|
||||
private String msg_intent;
|
||||
private boolean emailIsValid = false;
|
||||
private boolean fullnameIsValid = false;
|
||||
private boolean passwordIsValid = false;
|
||||
|
||||
private Button btn_login;
|
||||
private TextView input_email;
|
||||
private TextView input_full_name;
|
||||
private TextView input_username;
|
||||
private TextView input_password;
|
||||
|
||||
private ProgressDialog pDialog;
|
||||
@@ -64,15 +69,52 @@ public class LoginActivity extends AppCompatActivity implements FacebookButtonFr
|
||||
|
||||
if(msg_intent.equals("login")){
|
||||
setContentView(R.layout.activity_login);
|
||||
input_email = (TextView) findViewById(R.id.input_register_email);
|
||||
input_password = (TextView) findViewById(R.id.input_register_password);
|
||||
}else if(msg_intent.equals("register")){
|
||||
setContentView(R.layout.activity_register);
|
||||
input_full_name = (TextView) findViewById(R.id.input_register_name);
|
||||
input_email = (TextView) findViewById(R.id.input_register_email);
|
||||
input_password = (TextView) findViewById(R.id.input_register_password);
|
||||
input_username = (TextView) findViewById(R.id.input_register_name);
|
||||
|
||||
input_username.addTextChangedListener(new TextValidator(input_username) {
|
||||
@Override
|
||||
public void validate(TextView textView, String text) {
|
||||
if (text.length() < 5) {
|
||||
textView.setError("Your username must be at least\n" +
|
||||
"5 characters in length.");
|
||||
fullnameIsValid = false;
|
||||
} else {
|
||||
fullnameIsValid = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
input_email = (TextView) findViewById(R.id.input_register_email);
|
||||
input_password = (TextView) findViewById(R.id.input_register_password);
|
||||
|
||||
input_email.addTextChangedListener(new TextValidator(input_email) {
|
||||
@Override
|
||||
public void validate(TextView textView, String text) {
|
||||
if(!Patterns.EMAIL_ADDRESS.matcher(text).matches()){
|
||||
textView.setError("Please enter a valid email address\n" +
|
||||
"e.g.: text@abc.de");
|
||||
emailIsValid = false;
|
||||
}else{
|
||||
emailIsValid = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
input_password.addTextChangedListener(new TextValidator(input_password) {
|
||||
@Override
|
||||
public void validate(TextView textView, String text) {
|
||||
if(text.length() < 5) {
|
||||
textView.setError("Your password must be at least\n" +
|
||||
"5 characters in length.");
|
||||
passwordIsValid = false;
|
||||
}else{
|
||||
passwordIsValid = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
FragmentManager fragmentManager = getSupportFragmentManager();
|
||||
FacebookButtonFragment fragmentFB = new FacebookButtonFragment();
|
||||
@@ -95,16 +137,18 @@ public class LoginActivity extends AppCompatActivity implements FacebookButtonFr
|
||||
String password = input_password.getText().toString();
|
||||
|
||||
if (msg_intent.equals("login")) {
|
||||
if (isEmpty(input_email) || isEmpty(input_password)) {
|
||||
showMessage("Please enter all fields before logging in");
|
||||
if (!emailIsValid || !passwordIsValid) {
|
||||
showMessage("Entered fields not valid\n" +
|
||||
"Please check errors first.");
|
||||
} else {
|
||||
showDialog();
|
||||
checkLogin(email, password);
|
||||
}
|
||||
} else if (msg_intent.equals("register")) {
|
||||
String name = input_full_name.getText().toString();
|
||||
if (isEmpty(input_email) || isEmpty(input_password) || isEmpty(input_full_name)) {
|
||||
showMessage("Please enter all fields before registration");
|
||||
String name = input_username.getText().toString();
|
||||
if (!emailIsValid || !passwordIsValid || !fullnameIsValid) {
|
||||
showMessage("Entered fields not valid\n" +
|
||||
"Please check errors first.");
|
||||
} else {
|
||||
showDialog();
|
||||
registerUser(name, email, password);
|
||||
@@ -463,11 +507,7 @@ public class LoginActivity extends AppCompatActivity implements FacebookButtonFr
|
||||
JSONObject jObj = new JSONObject(response);
|
||||
boolean error = jObj.getBoolean("error");
|
||||
if (!error) {
|
||||
// do nothing actually
|
||||
} else {
|
||||
|
||||
// Error occurred in registration. Get the error
|
||||
// message
|
||||
String errorMsg = jObj.getString("error_msg");
|
||||
Toast.makeText(getApplicationContext(),
|
||||
errorMsg, Toast.LENGTH_LONG).show();
|
||||
@@ -545,9 +585,6 @@ public class LoginActivity extends AppCompatActivity implements FacebookButtonFr
|
||||
};
|
||||
}
|
||||
|
||||
private boolean isEmpty(TextView etText) {
|
||||
return etText.getText().toString().trim().length() == 0;
|
||||
}
|
||||
|
||||
public void showMessage(String statusText){
|
||||
Toast.makeText(this, statusText, Toast.LENGTH_LONG).show();
|
||||
@@ -575,4 +612,5 @@ public class LoginActivity extends AppCompatActivity implements FacebookButtonFr
|
||||
if (pDialog.isShowing())
|
||||
pDialog.dismiss();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -176,7 +176,6 @@ public class ViewReportActivity extends BaseActivity {
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
<string name="lbl_login_usesocialnet">Or use following social networks to sign in</string>
|
||||
|
||||
<string name="lbl_register_title">Register to RiskAhead</string>
|
||||
<string name="input_register_name_hint">Full Name</string>
|
||||
<string name="input_register_name_hint">Username</string>
|
||||
<string name="input_register_email_hint">E-Mail</string>
|
||||
<string name="input_register_password_hint">Password</string>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user