Hello, I’m working on this task:
Write a program that validates a password. It should read the password from the new line and check that it is at least 8 characters long and contains at least one uppercase letter, one lowercase letter, and one digit. If the input doesn’t meet these requirements, your program should output an error-specific message as follows:
“Password length should be 8 symbols or more.”
“Password should contain at least one uppercase letter.”
“Password should contain at least one lowercase letter.”
“Password should contain at least one digit.”
If the user violates several rules at once, write all error-specific messages that apply, each on a new line, in the same order as listed here. The program should continue to ask for and read the password until the user inputs a valid value. Once a valid value is given, the program should output: “Your password is properly set!” For example:
>secret
Password length should be 8 symbols or more.
Password should contain at least one uppercase letter.
Password should contain at least one digit.
>Secret
Password length should be 8 symbols or more.
Password should contain at least one digit.
>Secret8
Password length should be 8 symbols or more.
>Secret88
Your password is properly set!
Here is my code:
namespace InputValidation
{
class PasswordValidator
{
static void Main(string[] args)
{
string password;
bool isValid = false;
// You code here
while (!isValid)
{
Console.WriteLine("Please enter a password: ");
password = Console.ReadLine();
bool hasLength = (password.Length >= 8);
bool hasUpperCase = false;
bool hasLowerCase = false;
bool hasDigit = false;
for (int i = 0; i < password.Length; i++)
{
if (char.IsUpper(password[i]))
{
hasUpperCase = true;
}
else if (char.IsLower(password[i]))
{
hasLowerCase = true;
}
else if (char.IsDigit(password[i]))
{
hasDigit = true;
}
}
if (!hasLength)
{
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.");
}
if (hasLength && hasUpperCase && hasLowerCase && hasDigit)
{
Console.WriteLine("Your password is properly set!");
isValid = true;
}
}
}
}
}
Can anyone please help me to solve it?