-
Notifications
You must be signed in to change notification settings - Fork 0
/
Validator.cs
69 lines (62 loc) · 1.86 KB
/
Validator.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
// Auth: Christian B
// Date: 4/9/24
// Desc: Validator Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TaxApp_v2
{
internal class Validator
{
//A private static field to hold the title for error messages, initialized with "Entry Error"
private static string title = "Entry Error";
public static string Title
{
get => title;
set => title = value;
}
//Checks if the textbox has any text and displays an error message if not
public static bool IsPresent(TextBox textBox)
{
if (string.IsNullOrWhiteSpace(textBox.Text))
{
MessageBox.Show(textBox.Tag + " is a required field.", Title);
textBox.Focus();
return false;
}
return true;
}
//Validates that the text in the TextBox can be converted to a decimal
public static bool IsDecimal(TextBox textBox)
{
decimal number = 0m;
if (decimal.TryParse(textBox.Text, out number))
{
return true;
}
else
{
MessageBox.Show(textBox.Tag + " must be a decimal value", Title);
textBox.Focus();
return false;
}
}
// Validates that the text in the TextBox can be converted to an integer
public static bool IsInt32(TextBox textBox)
{
int number = 0;
if (int.TryParse(textBox.Text, out number))
{
return true;
}
else
{
MessageBox.Show(textBox.Tag + " must be an integer.", Title);
textBox.Focus();
return false;
}
}
}
}