I am making password validation method using console app and got stuck with comparing two chars. Is there a way to compare two chars without using toUpper() or toLower() method. For example if i am comparing these two chars
char c1 = 'a', c2 = 'A';
bool result = c1.Equals(c2);
I want result to be true.
I've tried using toLower() method but i have a problem.
This is my password validation Method.
private static bool PasswordValidation(string input)
{
if(!(input.Length>=8&&input.Length<=15))
{
Console.WriteLine("min 8 characters max 15");
return false;
}
int specialCharacters = 0, uppLatter = 0, lowerLatter = 0;
char[] charArray = input.ToCharArray();
for (int i = 0; i < charArray.Length; i++)
{
char ch = charArray[i];
if (char.IsWhiteSpace(ch))
{
Console.WriteLine("can't use space in password");
return false;
}
if (!char.IsLetterOrDigit(ch))
specialCharacters++;
if (char.IsUpper(ch))
uppLatter++;
if (char.IsLower(ch))
lowerLatter++;
if (i < charArray.Length - 1)
{
if (char.ToLower(charArray[i])==char.ToLower(charArray[++i]))
{
Console.WriteLine("same characters");
return false;
}
if(char.IsDigit(charArray[i])&&char.IsDigit(charArray[++i]))
{
int sum = Convert.ToInt32(charArray[i]) + Convert.ToInt32(charArray[++i]);
if(sum==input.Length)
{
Console.WriteLine("sum is equal to length");
return false;
}
}
}
}
if (specialCharacters == 0)
{
Console.WriteLine("at lesast one special character is required");
return false;
}
if (uppLatter == 0)
{
Console.WriteLine("at lesast one upper latter is required");
return false;
}
if (lowerLatter == 0)
{
Console.WriteLine("at lesast one lower character is required");
return false;
}
var repeats = input.GroupBy(s1 => s1)
.Where(s1 => s1.Count() > 3)
.Select(s1 => s1).ToArray();
if (repeats.Length > 0)
{
Console.WriteLine("one character can't be repeted more than 3 times");
return false;
}
return true;
}
As you can see i've used integers to count the number of lower and upper letters, as well as special characters but this will only work for the first character of input string. Soon as i hit this
if (char.ToLower(charArray[i])==char.ToLower(charArray[++i]))
line of code, all hell breaks loose.
Any suggestion is helpful. Thank you for your time.
Aucun commentaire:
Enregistrer un commentaire