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

前端 未结 4 2145
温柔的废话
温柔的废话 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 02:08

    To count uppercase letters and digits:

    string s = "some-password";
    int upcaseCount= 0;
    int numbersCount= 0;
    for (int i = 0; i < s.Length; i++)
    {
        if (char.IsUpper(s[i])) upcaseCount++; 
        if (char.IsDigit(s[i])) numbersCount++;
    }
    

    and check s.Length for the length

    good luck!

提交回复
热议问题