❔ Validating user input
I have created a project and I need to learn how to validate user inputs
ValidateUserInput method that.. prints an optional message, and if no message was provided... what is going on?string.IsNullOrWhitespace

bool indicating if the validation passed or failed, or throw an exception on validation failurebool approachList<string>Func<string, bool> I'm not at this level yet.Main is a method, not a programProgram itself.UserInput static class with these methods in itValidateUserInputstring.IsNullOrWhitespaceboolboolList<string>Func<string, bool>MainProgramUserInputpublic class UserAccount
{
public string FirstName { get; }
public string LastName { get; }
public int Age { get; }
public string EmailAddress { get; }
public string HomeAddress { get; }
public string PhoneNumber { get; }
public string ID { get; }
public UserAccount(string firstname, string lastname, string emailaddress, string homeaddress, string phonenumber, string id)
{
FirstName = firstname;
LastName = lastname;
EmailAddress = emailaddress;
HomeAddress = homeaddress;
PhoneNumber = phonenumber;
ID = id;
}
public static string ValidateUserInput(string validate)
{
if (validate != null)
{
Console.WriteLine(validate);
}
else
{
Console.WriteLine("Invalid input!");
}
return validate;
}public class UserAccount
{
public static bool ValidateName(string? name)
{
if (string.IsNullOrWhiteSpace(name))
{
Console.WriteLine("Invalid name: must be atleast one non-whitespace character");
return false;
}
return true;
}
}internal class Program
{
private static void Main(string[] args)
{
var validName = GetStringInput("Enter an account name: ", UserAccount.ValidateName);
}
public static string GetStringInput(string prompt, Func<string, bool> validator)
{
while (true)
{
Console.Write(prompt);
var input = Console.ReadLine();
if (validator(input))
{
return input;
}
Console.WriteLine("Bad input, try again.");
}
}
} public static string GetStringInput(string prompt, Func<string, bool> validator)
{
while (true)
{
Console.Write(prompt);
var input = Console.ReadLine();
if (validator(input))
{
return input;
}
Console.WriteLine("Bad input, try again.");
}
}public static T ParseInput<T>(string prompt, Func<T, bool> validator) where T : IParsable<T>
{
while (true)
{
Console.Write(prompt);
if (T.TryParse(Console.ReadLine(), null, out var result) && validator(result))
{
return result;
}
Console.WriteLine("Bad input, try again.");
}
}