Tuesday, October 14, 2008

Password setting in c#

If you mean that the password must be exactly 6 characters long and contain at least one uppercase letter, lowercase letter and digit and the other characters (if any) can be anything you like, then try this:

using System;

class Password
{
static void Main()
{
bool lower, upper, digit;
while(true)
{
lower = upper = digit = false;
Console.Write("Enter password : ");
string password = Console.ReadLine();
if (password.Length == 6)
{
foreach(char c in password)
{
if ('a' <= c && c <= 'z')
{
lower = true;
}
else if ('A' <= c && c <= 'Z')
{
upper = true;
}
else if ('0' <= c && c <= '9')
{
digit = true;
}
}
if (lower && upper && digit) break;
}
Console.WriteLine("Invalid, please re-enter");
}
}
}

No comments: