密码要求C#WPF

时间:2013-11-27 10:55:37

标签: c# wpf passwords requirements

制作程序,告诉您密码是否符合要求。 要求;

需要7个字母或更多。 第一个字母必须是B. 密码必须至少有3个数字。 密码必须至少有一个字母。

问题:我该如何解决这个问题?一直在使用谷歌没有运气。我不知道如何设定要求。

Lykilord表示密码。 这就是我到目前为止所做的:

    private void btathuga_Click(object sender, RoutedEventArgs e)
    {
        int lykilord = 0;
        if (lykilord > 7)
        {
            MessageBox.Show("Wrong!");
        }
        else if (true)
        {
            MessageBox.Show("Wrong!");
        }

3 个答案:

答案 0 :(得分:2)

让密码成为您从用户获得的字符串作为其密码。如果

,你会接受它
if(password.Length>=7 && 
   password[0]=='B' && 
   password.Where(x => Char.IsDigit(x)).Count()>=3 &&
   password.Where(x => Char.IsLetter(x)).Count()>1)
{
    // The password is ok. Here goes your code.
}

答案 1 :(得分:1)

这是我从你的问题中推断出来的,可能会有所帮助

 private void button1_Click(object sender, EventArgs e)
 {
   if (textBox1.Text.Length < 7)
        {
            MessageBox.Show("Password can't be less than 7 letter long !");
            return;
        }
        else if (!textBox1.Text.ToString().Substring(0, 1).Equals("B"))
        {
            MessageBox.Show("Password's first letter must be 'B'");
            return;
        }
        int countOfDigit = 0, countOfLetters = 0;
        foreach (var digit in textBox1.Text)
        {
            if (char.IsDigit(digit))
                ++countOfDigit;
            if (char.IsLetter(digit))
                ++countOfLetters;
        }
        if (countOfDigit < 3)
        {
            MessageBox.Show("Password must have atleast 3 digits");
            return;
        }

        if (countOfLetters < 1)
        {
            MessageBox.Show("Password must have atleast 1 Letter");
            return;
        }
        MessageBox.Show("Congratulations ! Your Password is as per Norms !");
    }

答案 2 :(得分:0)

你有一些要求 - 我会将它们分开。

  • 7个字母或更多

您可以使用

返回此内容
    lykilord.Length();
  • 首字母必须是B

    lykilord.Substring(0,1);
    
  • 密码必须至少包含3个数字

这里几乎没有选择。如果你使用C#我会使用LINQ,你可以

    int countOfNumbers = lykilord.Count(Char.IsDigit);
  • 密码必须至少包含1个字母

显然,如果开头有B,那么就有一封信,如果没有,则无效。但是,如果你想检查信件(以备将来参考)

    int countOfLetters = lykilord.Count(Char.IsLetter);

将所有这些结合在一起,您可以编译if语句:

    if (lykilord.Substring(0,1).ToUpper() == "B" && // the ToUpper() will mean they can have either lowercase or uppercase B
        lykilord.Length() > 7 &&
        countOfNumbers > 3 &&) 
        // i've excluded the letter count as you require the B
    {
         // password is good
    }
    else
    {
         // password is bad
    }

我已尽最大努力将其分成几部分,以便您从中学习 - 它看起来有点像一部分作业或学术作业的一部分。我们不是来为你做这个功课。

相关问题