forked from BYSuvNet/Scout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInput.cs
76 lines (69 loc) · 2.03 KB
/
Input.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
namespace ScoutApp.UI;
static class Input
{
public static char GetChar(string prompt = "", bool toUpper = true)
{
do
{
Console.Write(prompt);
char input = Console.ReadKey().KeyChar;
Console.WriteLine();
return toUpper ? char.ToUpper(input) : input;
} while (true);
}
public static string GetString(string prompt = "")
{
do
{
Console.Write(prompt);
string input = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(input))
{
return input;
}
Console.WriteLine("Du måste ange något!");
} while (true);
}
public static DateTime GetDateTime(string prompt = "")
{
do
{
Console.Write(prompt);
if (DateTime.TryParse(Console.ReadLine(), out DateTime date))
{
return date;
}
Console.WriteLine("Du måste ange ett datum!");
} while (true);
}
public static DateOnly GetDateOnly(string prompt = "")
{
do
{
Console.Write(prompt);
if (DateOnly.TryParse(Console.ReadLine(), out DateOnly date))
{
return date;
}
Console.WriteLine("Du måste ange ett datum!");
} while (true);
}
public static string GetEmail(string prompt = "")
{
do
{
Console.Write(prompt);
string email = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(email) && IsValidEmail(email))
{
return email;
}
Console.WriteLine("E-postadressen inte giltig!");
} while (true);
}
//https://www.reddit.com/r/csharp/comments/sbvlgp/is_using_systemnetmailmailaddress_enough_to/
private static bool IsValidEmail(string email)
{
return System.Text.RegularExpressions.Regex.IsMatch(email, @"^\w+([-+.']\w+)*@(\[*\w+)([-.]\w+)*\.\w+([-.]\w+\])*$");
}
}