在字符串中查找字符以检查密码复杂性c#



我试图用这种方式进行密码复杂性测试,但没有成功。

 private void button1_Click(object sender, EventArgs e)
        {
            string CapsChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string LowerChars = "abcdefghijklmnopqrstuvwxyz";
            string Digits = "0123456789";
            string SpecialChars = "#<>!~@";
            if (textBox1.Text.ContainsInvariant(CapsChars))
            {
                if (textBox1.Text.ContainsInvariant(LowerChars))
                {
                    if (textBox1.Text.ContainsInvariant(Digits))
                    {
                        if (textBox1.Text.ContainsInvariant(SpecialChars))
                        {
                            MessageBox.Show("ALL Ok");
                        }
                        else
                        {
                            MessageBox.Show("No special character found");
                        }
                    }
                    else
                    {
                        MessageBox.Show("No digit found");
                    }
                }
                else
                {
                    MessageBox.Show("No lower case character found");
                }
            }
            else
            {
                MessageBox.Show("No upper case character found");
            }
        }
public static class StringExt
    {
        public static bool ContainsInvariant(this string sourceString, string filter)
        {
            return sourceString.ToLowerInvariant().Contains(filter);
        }
    }

我喜欢这样一种测试方式,即密码必须有一个大写字符和一个小写字符,一个数字和一个特殊字符,密码长度必须大于8。正在寻找如何实现它的建议。只是想知道我们可以使用LINQ实现同样的目标吗?感谢

不确定这是最好的变体。比你的简单一点:

using System;
using System.Linq;
namespace PasswordCheck
{
    class Program
    {
        static void Main(string[] args)
        {
            CheckPasswordComplexity("abc");
            CheckPasswordComplexity("aB1#");
            CheckPasswordComplexity("ABCc");
        }
        static private void CheckPasswordComplexity(string text)
        {
            var hasUpperCase = text.Any(char.IsUpper);
            var hasLowerCase = text.Any(char.IsLower);
            var hasDigits = text.Any(char.IsDigit);
            var hasPuncutation = text.Any(char.IsPunctuation); // Convers more than your case. Check here: http://www.dotnetperls.com/char-ispunctuation
            if (!hasUpperCase)
            {
                Console.WriteLine("No upper case character found");
            }
            else if (!hasLowerCase)
            {
                Console.WriteLine("No lower case character found");
            }
            else if (!hasDigits)
            {
                Console.WriteLine("No digit found");
            }
            else if (!hasPuncutation)
            {
                Console.WriteLine("No special character found");
            }
            else
            {
                Console.WriteLine("ALL Ok");
            }
        }
    }

我想你需要重写ContainsInvariant方法

public static class StringExt
{
    public static bool ContainsInvariant(this string sourceString, string filter)
    {
        return sourceString.Any(c=>filter.Contains(c));
    }
}

以前的变体return sourceString.ToLowerInvariant().Contains(filter)忽略大写字母,搜索全字符串匹配,而不是单字母匹配

您需要在类代码之前添加using System.Linq;才能使用方法Any()

最新更新