0

I've been trying to find a way to do this but not had much luck so far.

Basically what I'm trying to do is limit the entries from the user so they can only enter 1 letter and 1 number using the Console.Readkey. So, for example, A1, E5, J9 etc. I want to avoid them entering like 55 or EE as this causes an error in my code. Is there an easy way to achieve this?

ItsOliMate
  • 51
  • 2
  • 7

2 Answers2

0

You need to write your own logic like you will check on every input value contains at least 1 number and 1 letter is true else false:

 string value = Console.ReadLine();
        //you can also check value.length and redirect if length greater than 2
        if (value.Length > 2)
        {
            Console.WriteLine("Please enter correct value");
            return;
        }
        if (value.Contains("Your Number"))
        {
            if (value.Contains("Your Letter"))
            {
                //your code goes here
            }
            else
            {
                Console.WriteLine("Please Enter Correct Value");
            }
        }
        else
        {
            Console.WriteLine("Please Enter Correct Value");
        }
  • Hi Owais, Thanks for your reply, I have similar code in my project at the moment. The Value.contains part, would it be possible to check if it contains a range of numbers or letters? – ItsOliMate Sep 19 '20 at 19:19
  • this logic works fine but if you want more specific to only check number or letter in one line of code you can use regex – owais shahab Sep 19 '20 at 19:40
  • this link will help you https://stackoverflow.com/questions/12884610/how-to-check-if-a-string-contains-any-letter-from-a-to-z – owais shahab Sep 19 '20 at 19:42
  • Regex.IsMatch(hello, @"^[a-zA-Z]+$"); now you have to try some regex formula by your own and please check above mentioned link. – owais shahab Sep 19 '20 at 19:43
0

This uses a GetChar method which requires that you pass a function to check whether the input is a character or a number. It won't allow you so proceed until a valid entry has been made.

using System;

class Program {
  public static void Main (string[] args) {
    string value = string.Empty;

    // Get a character, using char.IsLetter as the checking function...
    GetChar(ref value, char.IsLetter);

    // Get a number, using char.isNumber as the checking function... 
    GetChar(ref value, char.IsNumber);

    Console.WriteLine($"\nValue: {value.ToUpper()}");
  }

  // Get a character and append it to the referenced string.
  // check requires that you pass a function reference for the required check.
  public static void GetChar(ref string value, Func<char, bool> check) {

    // Loop until the check passes.
    while(true) {
      char key = Console.ReadKey(true).KeyChar;

      // If check passes...
      if(check(key)) {

        // Append the value
        value += key.ToString().ToUpper();

        // Print it...
        Console.Write(key.ToString().ToUpper());

        // Break out of the loop.
        break;
      }
    }
  }
}
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313