2
A
回答
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()
來確定字符串中是否存在指定字符數組中的至少一個字符。
1
strPassword.IndexOfAny(new char['!','@','#','$','%','^','&','*','(',')']);
您可以使用您正在查找的字符/符號的數組運行該程序。如果它返回大於-1的任何東西,你就會得到一個匹配。數字也可以做到這一點。
這將允許您準確地定義您要查找的內容,並且可以輕鬆地讓您根據需要排除某些字符。
2
使用正則表達式會非常靈活。您始終可以將其存儲在資源文件中,並在不更改代碼的情況下對其進行更改。
請參閱下面的密碼示例;
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);
相關問題
- 1. 檢查NSString包含字母和數字
- 2. 檢查數字和字母的密碼
- 3. 檢查一個字符串是否包含數字和字母
- 4. 檢查字符串是否包含python中的特殊字符
- 5. 檢查一個字符串是否只包含特殊字符
- 6. 驗證密碼字段是否包含特殊字符
- 7. JQuery - 確認密碼包含非字母數字字符
- 8. 需要幫助:RegEx中包含數字和特殊字符的密碼
- 9. Preapre字符串包含特殊字符
- 10. JavaScript正則表達式包含字母數字和特殊字符
- 11. 如何編寫包含字母數字和特殊字符的輸入匹配
- 12. 包含特殊字符
- 13. JSON包含特殊字符
- 14. 檢查字段是否包含SQL中的特殊字符
- 15. 密碼驗證(有字母數字和特殊){8}
- 16. PHP:特殊字符密碼
- 17. 如何檢查一個字符是否包含特定字母
- 18. 檢查鍵是字母/數字/特殊符號
- 19. 檢查字符串是否包含字母數字
- 20. PHP檢查字符串是否包含一些字母/數字
- 21. 允許一些特殊字符和字母數字字符
- 22. 如何檢查輸入字段,它只包含數字或特殊字符
- 23. 不包含特殊字符的字符串中的字符數
- 24. 包含特殊字符的字母順序perl的
- 25. 如何在Javascript中檢查字符串包含字母字符
- 26. 檢查字符串是否僅包含英文字母,數字和符號
- 27. 的Sql - 刪除包含特殊字符的塔(未字母數字)
- 28. Windows命令行命令在密碼中包含特殊字符
- 29. 限制密碼至少包含x個特殊字符?
- 30. 特殊字符包含在密碼中,它打破了URL
是什麼?如果((PasswordText.IndexOfAny(alpha.ToCharArray())> = 0)&&(PasswordText.IndexOfAny(digits。ToCharArray())> = 0)) { } – 001 2010-07-30 16:55:49
什麼是特殊字符? – 001 2010-07-30 16:56:20
@khou - 就是這個想法。你需要創建一個你想支持的特殊字符的char []。 – 2010-07-30 17:01:30