Android 带搜索栏的密码强度检查器

Android Password Strength checker with seekbar

如何在 android 中使用搜索栏创建密码强度检查器?

您可以使用 https://github.com/VenomVendor/Password-Strength-Checker 满足您的要求

或使用 TextWatcher 检查 EditText 长度。像这样

 public void afterTextChanged(Editable s)
                    {
                                 if(s.length()==0)
                                        textViewPasswordStrengthIndiactor.setText("Not Entered");
                                 else if(s.length()<6)
                                        textViewPasswordStrengthIndiactor.setText("EASY");
                                 else if(s.length()<10) 
                                        textViewPasswordStrengthIndiactor.setText("MEDIUM"); 
                                 else if(s.length()<15) 
                                        textViewPasswordStrengthIndiactor.setText("STRONG");
                                   else 
                                        textViewPasswordStrengthIndiactor.setText("STRONGEST");

                               if(s.length()==20)
                                   textViewPasswordStrengthIndiactor.setText("Password Max Length Reached");
                    }
            };

Demo Help .

afterTextChanged (Editable s) - This method is called when the text has been changed. Because any changes you make will cause this method to be called again recursively, you have to be watchful about performing operations here, otherwise it might lead to infinite loop.

  1. 为密码强度创建规则引擎,可能是一个简单的函数,当您将字符串传递给它时 returns 强度。
  2. 在您的密码编辑文本中使用 TextWatcher 并传递通过您的规则输入的任何字符串。
  3. 使用规则引擎返回的强度值来设置进度条的进度值和进度颜色。

https://github.com/yesterselga/password-strength-checker-android

是一个很好的例子。我更改了代码以不使用字符串值。我使用 0-4 的值代替它。这是代码

public enum PasswordStrength
{

    WEAK(0, Color.RED), MEDIUM(1, Color.argb(255, 220, 185, 0)), STRONG(2, Color.GREEN), VERY_STRONG(3, Color.BLUE);

    //--------REQUIREMENTS--------
    static int REQUIRED_LENGTH = 6;
    static int MAXIMUM_LENGTH = 6;
    static boolean REQUIRE_SPECIAL_CHARACTERS = true;
    static boolean REQUIRE_DIGITS = true;
    static boolean REQUIRE_LOWER_CASE = true;
    static boolean REQUIRE_UPPER_CASE = true;

    int resId;
    int color;

    PasswordStrength(int resId, int color)
    {
        this.resId = resId;
        this.color = color;
    }

    public int getValue()
    {
        return resId;
    }

    public int getColor()
    {
        return color;
    }

    public static PasswordStrength calculateStrength(String password)
    {
        int currentScore = 0;
        boolean sawUpper = false;
        boolean sawLower = false;
        boolean sawDigit = false;
        boolean sawSpecial = false;

        for (int i = 0; i < password.length(); i++)
        {
            char c = password.charAt(i);

            if (!sawSpecial && !Character.isLetterOrDigit(c))
            {
                currentScore += 1;
                sawSpecial = true;
            }
            else
            {
                if (!sawDigit && Character.isDigit(c))
                {
                    currentScore += 1;
                    sawDigit = true;
                }
                else
                {
                    if (!sawUpper || !sawLower)
                    {
                        if (Character.isUpperCase(c))
                            sawUpper = true;
                        else
                            sawLower = true;
                        if (sawUpper && sawLower)
                            currentScore += 1;
                    }
                }
            }
        }

        if (password.length() > REQUIRED_LENGTH)
        {
            if ((REQUIRE_SPECIAL_CHARACTERS && !sawSpecial) || (REQUIRE_UPPER_CASE && !sawUpper) || (REQUIRE_LOWER_CASE && !sawLower) || (REQUIRE_DIGITS && !sawDigit))
            {
                currentScore = 1;
            }
            else
            {
                currentScore = 2;
                if (password.length() > MAXIMUM_LENGTH)
                {
                    currentScore = 3;
                }
            }
        }
        else
        {
            currentScore = 0;
        }

        switch (currentScore)
        {
            case 0:
                return WEAK;
            case 1:
                return MEDIUM;
            case 2:
                return STRONG;
            case 3:
                return VERY_STRONG;
            default:
        }

        return VERY_STRONG;
    }

}

以及如何使用它:

if(PasswordStrength.calculateStrength(mViewData.mRegisterData.password). getValue() < PasswordStrength.STRONG.getValue())
{
    message = "Password should contain min of 6 characters and at least 1 lowercase, 1 uppercase and 1 numeric value";
    return null;
}

您可以使用 PasswordStrength.VERY_STRONG.getValue() 作为替代。或中