如何验证密码是否包含X个大写字母和Y个数字?
在C#中如何验证密码至少包含X个大写字母和至少Y个数字,并且整个字符串比Z长?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(4)
国粹2024-12-27 03:08:26
使用 LINQ Where() 方法 简洁明了:
int requiredDigits = 5;
int requiredUppercase = 5;
string password = "SomE TrickY PassworD 12345";
bool isValid = password.Where(Char.IsDigit).Count() >= requiredDigits
&&
password.Where(Char.IsUpper).Count() >= requiredUppercase;
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
密码强度:
首先,我会阅读密码强度,并仔细检查您的策略,以确保您做的是正确的事情(我无法立即告诉您):
然后我会检查其他问题:
然后我就开始说正事了。
实现:
您可以使用 Linq:
您还可以使用正则表达式(这可能是一个不错的选择,可以让您将来插入更复杂的验证):
或者您可以混合和匹配它们。
如果您必须多次执行此操作,您可以通过构建一个类来重用
Regex
实例: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):
Then I'd check other questions:
Then I'd get down to business.
Implementation:
You could use Linq:
You could use also regular expressions (which might be a good option to allow you to plug in more complicated validations in the future):
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: