在android中登录和注册页面验证

时间:2018-04-16 13:15:18

标签: java android database

我在Android中开发了一个登录表单和注册表单。我在这里使用验证,直到只登录表格。我必须填写任何人(用户名或密码),然后我的应用程序应显示成功!并应转移到其他活动。

但是,如果两个字段都为空,则不应显示成功消息,并且应显示登录失败!!!。

请帮帮我..

if(status.equals("Success!"))
    {
        // ADD  to save  and  read next time
        String strUserName = mEditText1.getText().toString().trim();
        String strPassword = mEditText2.getText().toString().trim();
        if (null == strUserName || strUserName.length() == 0)
        {
            // showToast("Enter Your Name");
            mEditText1.setError( "username is required!" );
            boolean isUserValidated = false;
        }
        if (null == strPassword || strPassword.length() == 0)
        {
            // showToast("Enter Your Password");
            //isPasswordValidated = false;
            mEditText2.setError( "password is required!" );
        }
    } 

2 个答案:

答案 0 :(得分:1)

如下所示?

if (etEmail.getText().toString().trim().isEmpty()) {
            tilEmail.setError(getString(R.string.validation_input_email));
            etEmail.requestFocus();
        } else if (!Patterns.EMAIL_ADDRESS.matcher(etEmail.getText().toString().trim()).matches()) {
            tilEmail.setError(getString(R.string.validation_input_email_invalid));
            etEmail.requestFocus();
        } else if (etPassword.getText().toString().trim().isEmpty()) {
            tilPassword.setError(getString(R.string.validation_input_password));
            etPassword.requestFocus();
        } else {
          //Call your API/function here
        }

答案 1 :(得分:0)

以下是验证代码。

//Call on button action 
if (validate())
   hitLogInAPI();


/**
 * Validate all cases of input fields before api hit.
 */
private Boolean validate() {
//email empty check.
if (TextUtils.isEmpty(etEmail.getText().toString())) {
    //Error Toast 
    etEmail.requestFocus();
    return false;
}
//valid email check.
else if (!Patterns.EMAIL_ADDRESS.matcher(etEmail.getText().toString().trim()).matches()) {
    //Error Toast
    etEmail.requestFocus();
    return false;
}
//password empty check.
else if (TextUtils.isEmpty(etPassword.getText().toString().trim())) {
    //Error Toast
    etPassword.requestFocus();
    return false;
}

//password range check.
else if (etPassword.getText().toString().trim().length() < 6
        || etPassword.getText().toString().trim().length() > 32) {
    //Error Toast
    etPassword.requestFocus();
    return false;
}
return true;}