mardi 19 juin 2018

Password check without using RegEx and/or lists/rules

I've searched the forums and all over google and have yet to find an answer to my question. I'd like to create a simple program with one single for loop with if/if else statements. Is this possible without using RegEx or lists/rules?

I'd like it to fit these requirements:

Length 8. At least one digit. At least one upper case. At least one lower case.

This is what I've come up with thus far and I don't understand why it doesn't work? The reason I have the if statements without else if's is because I want all error messages to printed if the conditions are not met. Apologies for dumb questions, I'm new to c#.

static void Main(string[] args)
{
    var password = Console.ReadLine();

    for (var i = 0; i < password.Length; ++i)
    {
        bool hasDigit = char.IsDigit(password[i]);
        bool hasUpperCase = char.IsUpper(password[i]);
        bool hasLowerCase = char.IsLower(password[i]);

        if(password.Length < 8)
        {
            Console.WriteLine("Password length should be 8 symbols or more.");
        }
        if(!hasUpperCase)
        {
            Console.WriteLine("Password should contain at least one uppercase letter.");
        }
        if(!hasLowerCase)
        {
            Console.WriteLine("Password should contain at least one lowercase letter.");
        }
        if(!hasDigit)
        {
            Console.WriteLine("Password should contain at least one digit.");
        }

        password = Console.ReadLine();
    }

    Console.WriteLine("Your password is properly set!");
    Console.ReadLine();
}

Aucun commentaire:

Enregistrer un commentaire