0

i was trying to do password complexity test like this way but it is not working.

 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);
        }
    }

i like to test in such a way that password must have one caps character and one lower case character and one digit and one special character and pwd length has to be more than 8. looking for suggestion how to achieve it. just wondering can we achieve the same using LINQ? thanks

3
  • 2
    Regex would be a much better option for what you're trying to do IMHO. See here for a C# example which is similar (but not exactly the same) to what you're trying to achieve and can be modified to meet your exact needs: stackoverflow.com/questions/3131025/strong-password-regex
    – kha
    Feb 18, 2015 at 8:48
  • @kha Regex is still English-dependent. I'm bit nervous about [a-zA-Z]
    – Vitaliy
    Feb 18, 2015 at 9:01
  • There are a few studies showing that password complexities like this actually decrease security. Prevailing wisdom suggests asking the user for a pass-phrase instead of a pass-word is a better option.
    – Sam Axe
    Feb 18, 2015 at 9:02

2 Answers 2

3

Not sure this is best variant. Just bit easier than yours:

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");
            }
        }
    }
1
  • 1
    This is much better because it works with any language. Feb 18, 2015 at 8:59
1

i suppose you need to rewrite ContainsInvariant method

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

previous variant return sourceString.ToLowerInvariant().Contains(filter) ignores upper case letters and search for full string match, not single letter match

you need add using System.Linq; before class code to use method Any()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.