2010-07-30 126 views

回答

12

试试这个:

bool result = 
    passwordText.Any(c => char.IsLetter(c)) && 
    passwordText.Any(c => char.IsDigit(c)) && 
    passwordText.Any(c => char.IsSymbol(c)); 

尽管您可能希望对“字母字符”,“数字”和“符号”的含义有一个更具体的定义,因为这些术语对不同的人意味着不同的事情,并且您不确定这些术语的定义是否与定义相符该框架使用。

我想你的意思是'a-z'或'A-Z',按数字表示'0-9',用符号表示任何其他可打印的ASCII字符。如果是的话,试试这个:

static bool IsLetter(char c) 
{ 
    return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); 
} 

static bool IsDigit(char c) 
{ 
    return c >= '0' && c <= '9'; 
} 

static bool IsSymbol(char c) 
{ 
    return c > 32 && c < 127 && !IsDigit(c) && !IsLetter(c); 
} 

static bool IsValidPassword(string password) 
{ 
    return 
     password.Any(c => IsLetter(c)) && 
     password.Any(c => IsDigit(c)) && 
     password.Any(c => IsSymbol(c)); 
} 

事实上,如果你的意思是别的东西,然后相应地调整上述方法。

1

可以使用String.IndexOfAny()来确定字符串中是否存在指定字符数组中的至少一个字符。

+0

是什么?如果((PasswordText.IndexOfAny(alpha.ToCharArray())> = 0)&&(PasswordText.IndexOfAny(digits。ToCharArray())> = 0)) { } – 001 2010-07-30 16:55:49

+0

什么是特殊字符? – 001 2010-07-30 16:56:20

+0

@khou - 就是这个想法。你需要创建一个你想支持的特殊字符的char []。 – 2010-07-30 17:01:30

1
strPassword.IndexOfAny(new char['!','@','#','$','%','^','&','*','(',')']); 

您可以使用您正在查找的字符/符号的数组运行该程序。如果它返回大于-1的任何东西,你就会得到一个匹配。数字也可以做到这一点。

这将允许您准确地定义您要查找的内容,并且可以轻松地让您根据需要排除某些字符。

2

使用正则表达式会非常灵活。您始终可以将其存储在资源文件中,并在不更改代码的情况下对其进行更改。

请参阅下面的密码示例;

http://regexlib.com/Search.aspx?k=password

0

这应该满足您的所有需求。它不使用正则表达式。

private bool TestPassword(string passwordText, int minimumLength=5, int maximumLength=12,int minimumNumbers=1, int minimumSpecialCharacters=1) { 
    //Assumes that special characters are anything except upper and lower case letters and digits 
    //Assumes that ASCII is being used (not suitable for many languages) 

    int letters = 0; 
    int digits = 0; 
    int specialCharacters = 0; 

    //Make sure there are enough total characters 
    if (passwordText.Length < minimumLength) 
    { 
     ShowError("You must have at least " + minimumLength + " characters in your password."); 
     return false; 
    } 
    //Make sure there are enough total characters 
    if (passwordText.Length > maximumLength) 
    { 
     ShowError("You must have no more than " + maximumLength + " characters in your password."); 
     return false; 
    } 

    foreach (var ch in passwordText) 
    { 
     if (char.IsLetter(ch)) letters++; //increment letters 
     if (char.IsDigit(ch)) digits++; //increment digits 

     //Test for only letters and numbers... 
     if (!((ch > 47 && ch < 58) || (ch > 64 && ch < 91) || (ch > 96 && ch < 123))) 
     { 
      specialCharacters++; 
     } 
    } 

    //Make sure there are enough digits 
    if (digits < minimumNumbers) 
    { 
     ShowError("You must have at least " + minimumNumbers + " numbers in your password."); 
     return false; 
    } 
    //Make sure there are enough special characters -- !(a-zA-Z0-9) 
    if (specialCharacters < minimumSpecialCharacters) 
    { 
     ShowError("You must have at least " + minimumSpecialCharacters + " special characters (like @,$,%,#) in your password."); 
     return false; 
    } 

    return true;   
} 

private void ShowError(string errorMessage) { 
    Console.WriteLine(errorMessage); 
} 

这是如何调用它的一个示例:

private void txtPassword_TextChanged(object sender, EventArgs e) 
{ 
    bool temp = TestPassword(txtPassword.Text, 6, 10, 2, 1); 
} 

这不测试的大写和小写字母,这可以是优选的混合物。要做到这一点,只需打破字符条件,其中(ch > 96 && ch < 123)是小写字母集合,(ch > 64 && ch < 91)是大写字母。

正则表达式更短,更简单(对于那些对它很好的人),并且在线有很多示例,但此方法更适合为用户定制反馈。

1

这很简单;

Regex sampleRegex = new Regex(@"(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{2,})$"); 
boolean isStrongPassword= sampleRegex.IsMatch(givenPassword); 
相关问题