如何验证密码包含点¯x大写字母和Y号码? [英] How to verify that the password contains X uppercase letters and Y numbers?
问题描述
我如何在密码中包含至少X个大写字母和至少Y编号C#验证,并且整个字符串的长度大于Z'
感谢。
密码强度:
首先,我想上阅读起来密码强度,并仔细检查你的政策,以确保你在做正确的事(我不能告诉你断手):
- http://en.wikipedia.org/wiki/Password_strength
- < A HREF =https://www.grc.com/haystack.htm> https://www.grc.com/haystack.htm
- 的 http://xkcd.com/936/ (开个玩笑,但良好的精神食粮)
然后我会检查其他问题:
< 。p>然后我正事
实施
您可以使用LINQ的:
返回password.Length> = Z
和;&安培; password.Where(char.IsUpper).Count之间的()> = X
和;&安培; password.Where(char.IsDigit).Count之间的()> = Y
;
您也可以使用正则表达式(这可能是一个不错的选择,让你插上更复杂在未来的验证):
返回password.Length> = Z
和;&安培;新的正则表达式([A-Z]),匹配(密码).Count之间的> = X
和;&安培;新的正则表达式([0-9]),匹配(密码).Count之间的> = Y
;
或者你可以混合和匹配。
如果你有这个多次这样做,你可以通过建立一个类重用正则表达式
实例:
公共类PasswordValidator
{
公共BOOL的IsValid(字符串密码)
{
返回password.Length> MinimumLength
和;&安培; uppercaseCharacterMatcher.Matches(密码).Count之间的
> = FewestUppercaseCharactersAllowed
和;&安培; digitsMatcher.Matches(密码).Count之间的> = FewestDigitsAllowed
;
}
公众诠释FewestUppercaseCharactersAllowed {搞定;组; }
公众诠释FewestDigitsAllowed {搞定;组; }
公众诠释MinimumLength {搞定;组; }
私人正则表达式uppercaseCharacterMatcher =新的正则表达式([A-Z]);
私人正则表达式digitsMatcher =新的正则表达式([A-Z]);
}
变种验证=新PasswordValidator()
{
FewestUppercaseCharactersAllowed = X,
FewestDigitsAllowed = Y,
MinimumLength = Z,
};
返回validator.IsValid(密码);
How do I verify in C# that the password contains at least X uppercase letters and at least Y numbers, and the entire string is longer than Z?
Thanks.
Password Strength:
First, I would read up on password strength, and double-check your policy to make sure you were doing the right thing (I couldn't tell you off hand):
- http://en.wikipedia.org/wiki/Password_strength
- https://www.grc.com/haystack.htm
- http://xkcd.com/936/ (a joke, but good food for thought)
Then I'd check other questions:
Then I'd get down to business.
Implementation:
You could use Linq:
return password.Length >= z
&& password.Where(char.IsUpper).Count() >= x
&& password.Where(char.IsDigit).Count() >= y
;
You could use also regular expressions (which might be a good option to allow you to plug in more complicated validations in the future):
return password.Length >= z
&& new Regex("[A-Z]").Matches(password).Count >= x
&& new Regex("[0-9]").Matches(password).Count >= y
;
Or you could mix and match them.
If you had to do this multiple times, you could reuse the Regex
instances by building a class:
public class PasswordValidator
{
public bool IsValid(string password)
{
return password.Length > MinimumLength
&& uppercaseCharacterMatcher.Matches(password).Count
>= FewestUppercaseCharactersAllowed
&& digitsMatcher.Matches(password).Count >= FewestDigitsAllowed
;
}
public int FewestUppercaseCharactersAllowed { get; set; }
public int FewestDigitsAllowed { get; set; }
public int MinimumLength { get; set; }
private Regex uppercaseCharacterMatcher = new Regex("[A-Z]");
private Regex digitsMatcher = new Regex("[a-z]");
}
var validator = new PasswordValidator()
{
FewestUppercaseCharactersAllowed = x,
FewestDigitsAllowed = y,
MinimumLength = z,
};
return validator.IsValid(password);
这篇关于如何验证密码包含点¯x大写字母和Y号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!