简体   繁体   English

如何在C#中没有特殊字符或数字的情况下用字母(az)询问用户输入

[英]How to ask for user input with letters (a-z) only without special characters or numbers in C#

I am trying to create a program that requires me to put a validation on the user inputs to only accept letters in a given array of 5. So basically if i am the user i am not allowed to input numbers or special characters, and if i do, i would get an error. 我正在尝试创建一个程序,要求我对用户输入进行验证,以仅接受给定数组5中的字母。因此,基本上,如果我是用户,则不允许输入数字或特殊字符,如果我这样做,我会得到一个错误。 Can someone help me out with this? 有人可以帮我解决这个问题吗? I have tried various searches around and i have not been able to find a solution. 我已经尝试过各种搜索,但无法找到解决方案。 I appreciate any help that i can get. 我感谢我能得到的任何帮助。 This is what i have so far. 这就是我到目前为止所拥有的。

class Program
{
    static void Main(string[] args)
    {
        char[] arr = new char[5];

        //User input
        Console.WriteLine("Please Enter 5 Letters only: ");

        for (int i = 0; i < arr.Length; i++)
        {
            arr[i] = Convert.ToChar(Console.ReadLine());
        }
        //display
        for(int i = 0; i<arr.Length; i++)
        {
            Console.WriteLine("You have entered the following inputs: ");
            Console.WriteLine(arrArray[i]);
        }
    }
}
 char[] arr = new char[5];

            //User input
            Console.WriteLine("Please Enter 5 Letters only: ");
            string s = "abcdefghijklmnopqrstuvwxyz";
            for (int i = 0; i < arr.Length;)
            {
                string sChar = Console.ReadLine().ToLower();
                if (s.Contains(sChar) && sChar.Length == 1)
                {
                    arr[i] = Convert.ToChar(sChar);
                    i++;
                }
                else
                {
                    Console.WriteLine("Please enter a character from A-Z");
                    continue;
                }
            }
            //display
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine("You have entered the following inputs: ");
                Console.WriteLine(arr[i]);
            }

在此输入图像描述

I suggest using a Regular Expression(Regex) for that. 我建议使用正则表达式(正则表达式)。

For numbers and letters, the correct Regex is: 对于数字和字母,正确的正则表达式为:

string numbersLettersRegex = @"^[a-zA-Z0-9\_]+$"

Then, you just check against that Regex: 然后,您只需对照该正则表达式进行检查:

if (Regex.isMatch(numbersLettersRegex, arr[i] 
{ 
    //do stuff
}

else 
{
    //print error Message
}

pass your string you're trying to validate in to this func: 将您要验证的字符串传递给此功能:

public bool checkYo(String myString)
{
    return Regex.IsMatch(myString, @"^[a-zA-Z]+$");
}

this will check az and AZ.. if you want just az, just get rid of the AZ part from the above function. 这将检查az和AZ ..如果你只想要az,只需从上面的函数中删除AZ部分。 hope this helps. 希望这可以帮助。

Try this 尝试这个

static void Main(string[] args) { static void Main(string [] args){

char[] arr = new char[5];
Console.WriteLine("Please Enter 5 Letters only: ");
string inputstring = Console.ReadLine();

if (Regex.IsMatch(inputstring, @"^[a-zA-Z]+$"))
{
    arr = inputstring.ToCharArray();
    Console.WriteLine("You have entered the following inputs: ");

    //display
    for (int i = 0; i < arr.Length; i++)
    {
        Console.Write(arr[i]);
    }
    Console.ReadLine();
}
else
{
    Console.WriteLine("Enter valid inputs and try again");
    Console.ReadLine();
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM