C
C#3y ago
Slem P

❔ How do I restrict user from using numbers in Name

bool newSt = true;


while (newSt)
try
{


Console.WriteLine("Please enter the number of users you want to enter");


int iUsers = Convert.ToInt32(Console.ReadLine());



string[,] array = new string[iUsers, 3];
for (int i = 0; i < iUsers; i++)
{
Console.Write("Please enter your name: ");
string sUsrName = Console.ReadLine();
Console.Write("Please enter your nationality: ");
String sUsrNationality = Console.ReadLine();

Console.Write("Please enter your city: ");
string sUsrHome = Console.ReadLine();

array[i, 0] = sUsrName;
array[i, 1] = sUsrNationality;
array[i, 2] = sUsrResidence;
Console.WriteLine($"Name: {array[i, 0]}\nNationality: {array[i, 1]}\nCity: {array[i, 2]}");
newSt = false;
}
}
catch (SystemException)
{
Console.WriteLine("\n----Incorrect input, please try again----\n");

}
bool newSt = true;


while (newSt)
try
{


Console.WriteLine("Please enter the number of users you want to enter");


int iUsers = Convert.ToInt32(Console.ReadLine());



string[,] array = new string[iUsers, 3];
for (int i = 0; i < iUsers; i++)
{
Console.Write("Please enter your name: ");
string sUsrName = Console.ReadLine();
Console.Write("Please enter your nationality: ");
String sUsrNationality = Console.ReadLine();

Console.Write("Please enter your city: ");
string sUsrHome = Console.ReadLine();

array[i, 0] = sUsrName;
array[i, 1] = sUsrNationality;
array[i, 2] = sUsrResidence;
Console.WriteLine($"Name: {array[i, 0]}\nNationality: {array[i, 1]}\nCity: {array[i, 2]}");
newSt = false;
}
}
catch (SystemException)
{
Console.WriteLine("\n----Incorrect input, please try again----\n");

}
7 Replies
Slem P
Slem POP3y ago
now in the part where i ask for name nationality and residence how do i restrict user from entering numbers?
LPeter1997
LPeter19973y ago
Once the user pressed return, you need to run a validation method on it to check that it only contains what you want. If invalid, you prompt that to the user and re-ask them for the data.
Slem P
Slem POP3y ago
Thanks but how tho. Couldnt find any useful guide online.
LPeter1997
LPeter19973y ago
The general scheme is
string name;
do
{
Console.Write("Enter a name: ");
name = Console.ReadLine();
} while (!IsValidName(name));
string name;
do
{
Console.Write("Enter a name: ");
name = Console.ReadLine();
} while (!IsValidName(name));
You need to write IsValidName based on your requirements If you need to check if a string contains no numbers, you can use char.IsDigit on each letter in the string
Slem P
Slem POP3y ago
Ok I'm done for today. Thank you tho.
ero
ero3y ago
name.Any(char.IsDigit) when
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server