Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Password_Validator
- {
- class Program
- {
- static void Main(string[] args)
- {
- ValidatePassword(Console.ReadLine());
- }
- static void ValidatePassword(string password)
- {
- string validation = ValidatePassBetweenSixAndTenCharacters(password);
- if (validation != null)
- {
- Console.WriteLine(validation);
- }
- string validationTwo = ValidateContainsOnlyDigitsAndLetters(password);
- if (validationTwo != null)
- {
- Console.WriteLine(validationTwo);
- }
- string validationThree = ValidateHasLeastTwoDigits(password);
- if (validationThree != null)
- {
- Console.WriteLine(validationThree);
- }
- if (validation == null && validationTwo == null && validationThree == null)
- {
- Console.WriteLine("Password is valid");
- }
- }
- static string ValidatePassBetweenSixAndTenCharacters(string password)
- {
- int length = password.Length;
- string res = null;
- if(length < 6 || length > 10)
- {
- res = "Password must be between 6 and 10 characters";
- }
- return res;
- }
- static string ValidateContainsOnlyDigitsAndLetters(string password)
- {
- string res = null;
- for (int i = 0; i < password.Length; i++)
- {
- if(!char.IsLetterOrDigit(password[i]))
- {
- res = "Password must consist only of letters and digits";
- break;
- }
- }
- return res;
- }
- static string ValidateHasLeastTwoDigits(string password)
- {
- int counter = 0;
- string res = null;
- foreach (char c in password)
- {
- if (char.IsDigit(c))
- {
- counter++;
- }
- if (counter == 2)
- {
- break;
- }
- }
- if (counter < 2)
- {
- res = "Password must have at least 2 digits";
- }
- return res;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement