密码验证 - 添加其他要求 [英] Password validation - adding additional requirments

查看:157
本文介绍了密码验证 - 添加其他要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码目前检查最小和最大lentgh。我还想要大写,小写,特殊字符和数字。关于添加什么或在哪里可以找到一些例子的任何建议?我一直在谷歌搜索并通过这个论坛寻找,并一直在尝试添加额外的密码要求,并且一直没有成功。

I have some code that currently checks for minimum and maximum lentgh. I want to also require uppercase, lowercase, special char, and numeric. Any suggestions on what to add to this or where I can find some examples? I've been Googling and looking thru this forum and have been trying to add the additional password requirments and have been unsuccessful.

这就是我想要的。

长度至少为8个字符
长度不超过20个字符
至少小写字母和一个大写字母
at至少有一个特殊字符来自:!@#$%^& *()~` - = _ + [] {} |:;',。/<>?
至少有一个数字[0- 9]字符
无法匹配帐户登录名或电子邮件地址

At least eight characters in length No more than 20 characters in length at least lower-case letter and one upper-case at least one special character from: !@#$%^&*()~`-=_+[]{}|:";',./<>? at least one number [0-9] character Cannot match the account login name or email address

我当前的密码验证码

public static final int MIN_PASSWORD_LENGTH = 8;
public static final int MAX_PASSWORD_LENGTH = 20;

public static boolean isAcceptablePassword(String password)
{
    if(TextUtils.isEmpty(password))
        return false;

    int len = password.length();

    if(len < MIN_PASSWORD_LENGTH || len > MAX_PASSWORD_LENGTH)
        return false;

    for(int i = 0; i < len; i++)
    {
        char c = password.charAt(i);
        if (Character.isWhitespace(c))
            return false;
    }

    return true;
}


推荐答案

当你在分析时code> String 数据,你应该删除左右两边的空格。这是通过 Strimg#trim 函数完成的,如下所示:

When you're analyzing String data, you should erase the whitespaces on the right and left. This is done by the Strimg#trim function like this:

password = password.trim();

要分析String的每个字符,你可以将它转换为char数组,所以它将是更容易满足您的要求:

To analize every character of the String, you can transform it to a char array, so it will be easier to fulfill your requirements:

char[] arrPassword = password.toCharArray();

现在您可以使用以下函数评估char: Character#isUpperCase 字符#isLowerCase 字符#isDigit

Now you can evaluate a char using these functions: Character#isUpperCase, Character#isLowerCase, Character#isDigit.

最后但并非最不重要的是,您可以使用需要检查的特殊字符的String,并检查您正在评估的实际字符是否在该String内。这可以使用 String #indexOf String#valueOf 来实现,这可以将char转换为String类型。

Last but not least, you can have a String with the special characters you need to check, and check if the actual character you're evaluating is inside that String. This could be achieved using String#indexOf and String#valueOf, this las one to convert the char to a String type.

以下是所有这些解释的代码示例:

Here is a code sample for all this explanation:

public static final String SPECIAL_CHARACTERS = "!@#$%^&*()~`-=_+[]{}|:\";',./<>?";
public static final int MIN_PASSWORD_LENGTH = 8;
public static final int MAX_PASSWORD_LENGTH = 20;

public static boolean isAcceptablePassword(String password) {
    if (TextUtils.isEmpty(password)) {
        System.out.println("empty string.");
        return false;
    }
    password = password.trim();
    int len = password.length();
    if(len < MIN_PASSWORD_LENGTH || len > MAX_PASSWORD_LENGTH) {
        System.out.println("wrong size, it must have at least 8 characters and less than 20.");
        return false;
    }
    char[] aC = password.toCharArray();
    for(char c : aC) {
        if (Character.isUpperCase(c)) {
            System.out.println(c + " is uppercase.");
        } else
        if (Character.isLowerCase(c)) {
            System.out.println(c + " is lowercase.");
        } else
        if (Character.isDigit(c)) {
            System.out.println(c + " is digit.");
        } else
        if (SPECIAL_CHARACTERS.indexOf(String.valueOf(c)) >= 0) {
            System.out.println(c + " is valid symbol.");
        } else {
            System.out.println(c + " is an invalid character in the password.");
            return false;
        }
    }
    return true;
}

System.out.println(c +是密码中的无效字符。); 句子只是检查分析实际字符的结果。

The System.out.println(c + " is an invalid character in the password."); sentence is just to check the result of analyze the actual character.

这篇关于密码验证 - 添加其他要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆