密码最小和最大字符

时间:2014-03-25 15:01:29

标签: android if-statement passwords

我试图拥有我想在Android应用中设置的最大密码字符。编码它的最佳方法是什么?我只有至少6个字符,我想拥有最多10个字符集密码。这是我的尝试:

 public void UserSignUp() {
    if(ET_PASSWORD_SIGNUP.getText().length() >0 && ET_CONFIRM_PASSWORD_SIGNUP.getText().length() >0 &&
            ET_PASSWORD_SIGNUP.getText().length() >5 && ET_CONFIRM_PASSWORD_SIGNUP.getText().length() >5)
    {
        if(ET_PASSWORD_SIGNUP.getText().toString().equals(ET_CONFIRM_PASSWORD_SIGNUP.getText().toString())) {
        String SignUpResult = "";
        db = DBMANAGER.getWritableDatabase();
        SignUpResult = CONTENTMANAGER.saveUserPassword(ET_PASSWORD_SIGNUP.getText().toString(),this);
        Toast.makeText(getApplicationContext(), SignUpResult, Toast.LENGTH_SHORT).show();
         ActivityLogIn();
        } else {
            Toast.makeText(getApplicationContext(), "Confirm Password and Password did not match!",
                    Toast.LENGTH_SHORT).show();
        }
    } else {
        Toast.makeText(getApplicationContext(), "Invalid Input", Toast.LENGTH_SHORT).show();
    }
}

4 个答案:

答案 0 :(得分:2)

不是这样的答案,而是另一种解决方案,

查看https://github.com/ragunathjawahar/android-saripaar,漂亮的注释库。

您可以注释密码字段,它将为您处理逻辑......

@Password(order = 1, minLength = 4, maxLength = 10)
private EditText passwordEditText;

@ConfirmPassword(order = 2)
private EditText confirmPasswordEditText;

答案 1 :(得分:0)

private boolean checkPW(String pw, String pw2) {
        if(!pw.equals(pw2)){
            Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.error_pws_do_not_match), Toast.LENGTH_LONG).show();
            return false;
        }

        if (pw.length() < 4) {
            Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.error_pw_too_short), Toast.LENGTH_LONG).show();
            return false;
        }

        if(pw.length() > 10){
            Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.error_pw_too_long), Toast.LENGTH_LONG).show();
            return false;
        }

        //continue here
            return true;
            }
        });
    }

答案 2 :(得分:0)

检查此代码..

if (ET_PASSWORD_SIGNUP.getText().toString().length() < 6
            || ET_PASSWORD_SIGNUP.getText().toString().length() > 10) {

        ET_PASSWORD_SIGNUP.requestFocus();
        ET_PASSWORD_SIGNUP.setError("Password should be between 6 to 10 characters long");
}

答案 3 :(得分:0)

你可以写下这段代码:

// check password
public boolean checkPassword(String password, String confirmPassword) {
    boolean state = false;

    int passLength = password.length();
    if (passLength >= 6) {
        if (confirmPassword.length() != 0) {
            if (password.equals(confirmPassword)) {
                state = true;
            } else {
                makeToast("Password does't match!");
                inputConfirmPassword.setText("");
                inputPassword.setText("");
                inputPassword.requestFocus();
            }
        } else {
            Toast.makeText(this, "Please enter confirm password",
                    Toast.LENGTH_SHORT).show();
            inputConfirmPassword.setText("");
            inputConfirmPassword.requestFocus();
            state = false;
        }
    } else {
        Toast.makeText(this, "Please enter 6 digit password",
                Toast.LENGTH_SHORT).show();
        inputPassword.setText("");
        inputPassword.requestFocus();
        state = false;
    }
    return state;
}