1

The policies im trying to apply are that it has to be 3 characters long, that it must include a lower case letter, an upper case letter and a number in any order. Im trying to use it as a method to read in a password set by the user for an assignment.I have written as much as i can to emiminate errors but i cant figure out a way to apply said policies. this is what i have so far... Any help would be greatly appreciated

    private static string readPass() { //readpass method to read in password of 1 lower case, 1 upper case & 1 number


        bool validInput = true;
        char letterUserInput;                                           //letter from string
        int asciiCodeLetter;                                            //number code for letter
        string userInput;

        do {
            validInput = true;
            userInput = Console.ReadLine();
        try {

                if (userInput.Length < 1) {
                    validInput = false;
                    Console.WriteLine("You have just pressed Enter Please Enter a valid Password");
                }                      //if check length

                else if (userInput.Length > 1 && userInput.Length < 3) {
                    validInput = false;
                    Console.WriteLine("Password Is Too Short. Please Enter Valid Password");
                }                                                       //if check length too short

                else if (userInput.Length > 3)
                {
                    validInput = false;
                    Console.WriteLine("Password Is Too Long. Please Enter A Valid Password");
                }                                                       //if check length too short

                for (int i = 0; i < userInput.Length; i++)              //take each letter in turn from string
                {
                    letterUserInput = userInput[i];
                    asciiCodeLetter = Convert.ToInt16(letterUserInput);
                    if (asciiCodeLetter < 48 || asciiCodeLetter > 57)
                    {
                        validInput = false;
                        Console.WriteLine("You have used an invalid character.\n Password Must Contain 1 Upper Case, 1 Lower Case letter and 1 Number)");
                    }                                                    //character check with ASCII
                }
            }
            catch
            {
                validInput = false;
                Console.WriteLine("An unspecified error has occured");
            }                                                           //catch

            if (validInput == false) Console.Write("Invalid Password. Please Enter Again = ");

        } while (validInput == false);                                  // do. repeat entry if invalid

        return userInput;
    }
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
Fee Magee
  • 25
  • 5
  • You could eliminate the `userInput.Length > 1 && ` in the second else if. Because that block will only be executed if first `if` is not satisfied. – wingerse Nov 20 '15 at 22:47
  • why don't you try this link.. there is a c# example as well as a 1 liner example using `Regex` http://stackoverflow.com/questions/9477906/password-must-be-8-characters-including-1-uppercase-letter-1-special-character – MethodMan Nov 20 '15 at 22:51

4 Answers4

0

Some pseudocode for this might be:

String candidatePwd = textboxPwd.Text.Trim();
Boolean validPwd = IsValidPassword(candidatePwd);

private bool IsValidPassword(String candidate)
{
    return ((candidate.Length == 3) &&
            (ContainsOneUppercaseLetter(candidate)) &&
            (ContainsOneLowerCaseLetter(candidate)) &&
            (ContainsOneNumber(candidate)));
}

private bool ContainsOneUppercaseLetter(String candidate)
{
   // search for an uppercase letter; if one found, return true
}

private bool ContainsOneLowercaseLetter(String candidate)
{
   // search for a lowercase letter; if one found, return true
}

private bool ContainsOneUppercaseLetter(String candidate)
{
   // search for a number; if one found, return true
}

The actual implementation is left to you.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
0

You can use regular expressions for this:

var input = Console.ReadLine();
var regex = new System.Text.RegularExpressions.Regex("^([0-9][a-z][A-Z]|[0-9][A-Z][a-z]|[a-z][0-9][A-Z]|[a-z][A-Z][0-9]|[A-Z][a-z][0-9]|[A-Z][0-9][a-z])$");
validInput = input != null && regex.IsMatch(input);

I'm sure there are more fancy Regex for this. But this is simply enough to understand it if you are not familiar with Regex expressions.

Edit

You can use the following Regex if you want something more fancy:

 var input = Console.ReadLine();
 var regex = new System.Text.RegularExpressions.Regex(@"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{3}$");
 validInput = input != null && regex.IsMatch(input);
Fee Magee
  • 25
  • 5
Cat_Clan
  • 352
  • 2
  • 9
0

For 3 chars - simply save the password written by user as a string, then check it length. If it comes to upper case, I would convert every char on ASCII code, then check if any code is equal to the code of all upper case letters. For checking if there is a number, check every char of your password using something like this

int n; bool isNumeric = int.TryParse("123", out n);

Hope that will help.

0

You can use the regular expression:

^.(?=.{3,})(?=.\d)(?=.[a-z])(?=.[A-Z]).*$

and simplify your code:

if(!Regex.IsMatch(userInput, "^.*(?=.{7,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$")
  Console.WriteLine("The password has to be 3 characters long, and must include a lower case letter, an upper case letter, and a number");
toddmo
  • 20,682
  • 14
  • 97
  • 107