2010-07-30 154 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); 
相關問題