How to verify that the password contains X uppercase letters and Y numbers?

前端 未结 4 2141
温柔的废话
温柔的废话 2021-02-09 01:31

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.

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-09 01:52

    Short and clear using LINQ Where() method:

    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;
    

提交回复
热议问题