我正在为一个 android 应用程序制作注册表单,并且我正在尝试在用户单击注册按钮时使用 setError 功能来验证文本字段。它正在通过检查,但仍在将表单提交到我的服务器进行注册。在提交注册用户的请求之前,我希望所有字段都有效。我一直在寻找解决方案几个小时,但就是不知道我做错了什么。请帮忙!
btnRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String first_name = inputFirstName.getText().toString();
String last_name = inputLastName.getText().toString();
String email = inputEmail.getText().toString();
String dob = displaytxt.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.registerUser(first_name, last_name, email, dob, password);
// check for login response
try {
if(inputFirstName.getText().toString().length() <= 0)
{
inputFirstName.setError("First name required!");
}
if (inputLastName.getText().toString().length() < 0)
{
inputLastName.setError("Last name required!");
}
if (inputEmail.getText().toString().length() == 0)
{
inputEmail.setError( "Valid email address required!" );
}
if (inputPassword.getText().toString().length() < 8)
{
inputPassword.setError( "Password required! (Minimum of 8 characters." );
}
if (!inputPassword2.getText().toString().equals(inputPassword.getText().toString()))
{
inputPassword.setError( "Passwords do not match! (Minimum of 8 characters.");
inputPassword2.setError("Passwords do not match! (Minimum of 8 characters.");
}
if (!checkBoxTerms.isChecked());
{
AlertDialog alertDialog = new AlertDialog.Builder(RegisterActivity.this).create();
alertDialog.setTitle("oops!");
alertDialog.setMessage("You must agree to the terms & conditions.");
alertDialog.setButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
alertDialog.show();
}
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_FIRST_NAME), json_user.getString(KEY_LAST_NAME), json_user.getString(KEY_EMAIL), json_user.getString(KEY_DOB), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), MainActivity.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("Error in registration! Please check the information entered.");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});