I've worked on a little console application in C# to get started with the language. My goal is to ask the following from user:
- First name
- Surname
- Year of birth
- Month of birth
- Day of birth
I have made all of the input fields like following:
System.Console.Write("Name: ");
String name = System.Console.ReadLine();
In the end, the application saves the data to a .txt file, if the given data is valid. I need to check if the length of the name fields is between 1-30, and that the date inputs only accept numeric answers within their corresponding limitations (for e.g: You can only give 'month' a value between 1-12..)
I've tried to search for different validating methods, but I do not know how to throw them all together and make a clean "Checker" -part for this application.
This is what validated my first and last name fields, but I don't think you can put the date fields to the same checkup?
public static Boolean Checker(String check)
{
if (check.Length <= 30 && check.Length > 0)
{
return true;
}
return false;
}
Any advice?
int.TryParse()on each input (year, month, day) and will fail immediately in case any of those are not actually a number. Then checks if the value falls within a valid range (1-30, 1-12, 1-2016) and only then will try to check for the actual validity of the date usingDateTime.TryParse()