Password Checker improvement

118 views Asked by At

1.Problem...Its my password validation.COnsoleApp But i have bug....if first space is null program working(...but i want return false if first char is white

2.Many Condition using I wanna few using condition..i wanna practise good way

        {
            bool symb = false;
            bool letdig = false;
            char currentchar;
            char currentchar2;
            if (!(pass.Length >= 8 && pass.Length <= 25))
            {
                return true;
            }
            string symbols = "!@#$%^&*()_-+=[{]};:<>|./?.";
            char[] simbolchar = symbols.ToCharArray();


            for (int j = 0; j < pass.Length; j++)
            {
                currentchar = pass[j];
                foreach (var simb in simbolchar)
                {
                    if (simb == currentchar)
                    {
                        symb = true;
                    }
                }
                if (symb)
                {
                    for (int i = 0; i < pass.Length; i++)
                    {
                        currentchar2 = pass[i];
                        if (char.IsUpper(currentchar2) && (char.IsLetterOrDigit(currentchar2)))
                        {
                            letdig = true;
                        }
                    }
                }
                if (letdig)
                {
                    Console.WriteLine("WELCOME");
                    return true;
                }
            }
            return letdig;

        }
1

There are 1 answers

0
Mohammad Yaser Ammar On

Solve the first bug can be by adding a condition to check if the string is null or empty before any check after that

        public static bool passwordCheck(string pass)
        {
        bool symb = false;
        bool letdig = false;
        char currentchar;
        char currentchar2;
        //Solve the first bug
        if (string.IsNullOrEmpty(pass))
        {
            return false;
        }
        else
        {
            if (!(pass.Length >= 8 && pass.Length <= 25))
            {
                return true;
            }
            string symbols = "!@#$%^&*()_-+=[{]};:<>|./?.";
            char[] simbolchar = symbols.ToCharArray();


            for (int j = 0; j < pass.Length; j++)
            {
                currentchar = pass[j];
                foreach (var simb in simbolchar)
                {
                    if (simb == currentchar)
                    {
                        symb = true;
                    }
                }
                if (symb)
                {
                    for (int i = 0; i < pass.Length; i++)
                    {
                        currentchar2 = pass[i];
                        if (char.IsUpper(currentchar2) && (char.IsLetterOrDigit(currentchar2)))
                        {
                            letdig = true;
                        }
                    }
                }
                if (letdig)
                {
                    Console.WriteLine("WELCOME");
                    return true;
                }
            }
            return letdig;
        }
    }

As for code optimization, you can reading about clear code for your case