2014-03-25 79 views
0

我想要在我的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(); 
    } 
} 

回答

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; 
      } 
     }); 
    } 
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"); 
} 
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; 
} 
2

不是答案本身,而是另一種解決方案,

看一看https://github.com/ragunathjawahar/android-saripaar,很好的註釋庫。

您可以標註您的密碼字段,它會處理邏輯你...

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

@ConfirmPassword(order = 2) 
private EditText confirmPasswordEditText; 
+0

這個庫是不是即使是現在維護。雖然我喜歡這個概念。您只需編寫用於驗證的註釋並根據您的要求將參數傳遞給它,或者只需跳過它即可。 –

相關問題