0

For example if I ask the user to give me his name (string) and he writes numbers, symbols or if he press ENTER I want a loop to tell him to write string in order to continue. I made a loop for integers but I don't know how to make for strings

Console.Write("Please enter the name of the student: ");
//here I made the input to turn into Capitals
name = Console.ReadLine().ToUpper(); 

Console.Write("Please enter their student number: ");
// here I set a condition order to continue. First the input must be integer and second it must be positive
while (!int.TryParse(Console.ReadLine(), out id)||id<0)
{
    Console.ForegroundColor = ConsoleColor.Red;
    Console.Write("The value must be of integer type, try again: ");
    Console.ResetColor();
}
Misa Lazovic
  • 2,805
  • 10
  • 32
  • 38
Ainigma100
  • 61
  • 2
  • 10

1 Answers1

2
Console.Write("Please enter the name of the student: ");
//here I made the input to turn into Capitals
name = Console.ReadLine().ToUpper(); 
if (Regex.IsMatch(name, @"^[a-zA-Z]+$")) { // If letters only
 // Do something
}else{
 Console.WriteLine("Name must contain letters only");
}

Console.Write("Please enter their student number: ");
// here I set a condition order to continue. First the input must be integer and second it must be positive
while (!int.TryParse(Console.ReadLine(), out id)||id<0)
{
    Console.ForegroundColor = ConsoleColor.Red;
    Console.Write("The value must be of integer type, try again: ");
    Console.ResetColor();
}

This should check for letters only.

ZetaRift
  • 332
  • 1
  • 9
  • Because i am new to programming can you help me insert the code properly. I tried it, but i couldn't make a loop until the user enters correct input. how can i write it properly to my code? – Ainigma100 Jan 25 '16 at 23:54
  • Revised the code to basically check if `name` contains only letters. – ZetaRift Jan 26 '16 at 00:05