-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathValidationHelper.cs
More file actions
144 lines (122 loc) · 4.43 KB
/
ValidationHelper.cs
File metadata and controls
144 lines (122 loc) · 4.43 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
namespace Common;
using System.Globalization;
using System.Text.RegularExpressions;
public static class ValidationHelper
{
private static readonly string[] DateFormats = ["yyyyMMdd", "yyyyMM", "yyyy", "yyyy-MM-dd", "dd/MM/yyyy HH:mm:ss", "d/MM/yyyy hh:mm:ss tt", "dd/MM/yyyy HH:mm:ss tt"];
private static readonly string NilReturnFileNhsNumber = "0000000000";
/// <summary>
/// Validates that a date is not in the future and
/// in a valid format.
/// </summary>
/// <returns>bool, whether or not the date is valid</returns>
/// <remarks>
/// If you create a new date, make sure to provide a valid format in
/// the ToString parameter, otherwise it will default to the default
/// based on the culture info.
/// </remarks>
public static bool ValidatePastDate(string dateString)
{
var date = ParseDate(dateString);
if (date.HasValue)
{
return date < DateTime.UtcNow.Date.AddDays(1);
}
return false;
}
public static bool ValidateNHSNumber(string nhsNumber)
{
// Check the NHS number is a number
if (!long.TryParse(nhsNumber, out _))
{
return false;
}
if (nhsNumber.Length != 10)
{
return false;
}
// Check if NHS number is from a nil return file
if (nhsNumber == NilReturnFileNhsNumber)
{
return false;
}
//check digit (checksum) -- https://www.datadictionary.nhs.uk/attributes/nhs_number.html
int sum = 0;
int factor = 10;
for (int i = 0; i < 9; i++)
{
int digit;
if (!ParseInt32(nhsNumber[i], out digit))
{
return false;
}
sum += digit * factor;
factor--;
}
string checkDigit = (11 - (sum % 11)).ToString();
if (checkDigit == "10") return false;
if (checkDigit == "11") checkDigit = "0";
if (nhsNumber[9].ToString() == checkDigit)
{
return true;
}
return false;
}
/// <summary>
/// Validates the postcode according to the offical rules for valid
/// UK postcodes, also accepts dummy postcodes as valid.
/// </summary>
/// <param name="postcode">Postcode string (not null)</param>
/// <returns>bool, whether or not the postcode is valid</returns>
/// <remarks>
/// further information for postcode validation can be found in
/// ADR-008 on confluence.
/// </remarks>
public static bool ValidatePostcode(string postcode)
{
string validPostcodePattern = "^([A-Za-z][A-Ha-hJ-Yj-y]?[0-9][A-Za-z0-9]? ?[0-9][A-Za-z]{2}|[Gg][Ii][Rr] ?0[Aa]{2})$";
string dummyPostcodePattern = "^ZZ99 ?[0-9][A-Z]{2}$";
Match validPostcodeMatch = Regex.Match(postcode, validPostcodePattern, RegexOptions.IgnoreCase, TimeSpan.FromSeconds(2));
Match dummyPostcodeMatch = Regex.Match(postcode, dummyPostcodePattern, RegexOptions.IgnoreCase, TimeSpan.FromSeconds(2));
if (validPostcodeMatch.Success || dummyPostcodeMatch.Success)
return true;
return false;
}
/// <summary>
/// Gets the outcode (1st part of postcode) from the postcode.
/// </summary>
/// <param name="postcode">a non-null string representing the postcode</param>
/// <remarks>
/// Works for valid UK postcodes and dummy postcodes.
/// Works with or without a space separator between outcode and incode.
/// </remarks>
public static string? ParseOutcode(string postcode)
{
string pattern = @"^([A-Za-z][A-Za-z]?[0-9][A-Za-z0-9]?) ?[0-9][A-Za-z]{2}$";
Match match = Regex.Match(postcode, pattern, RegexOptions.IgnoreCase, TimeSpan.FromSeconds(2));
if (!match.Success)
{
return null;
}
string outcode = match.Groups[1].Value;
return outcode;
}
private static bool ParseInt32(char value, out int integerValue)
{
integerValue = (int)char.GetNumericValue(value);
if (integerValue < 0 || integerValue > 9)
{
integerValue = -1;
return false;
}
return true;
}
private static DateTime? ParseDate(string dateString)
{
if (DateTime.TryParseExact(dateString, DateFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date))
{
return date;
}
return null;
}
}