Skip to content

Commit b97887c

Browse files
authored
Merge pull request #3387 from TechnologyEnhancedLearning/Develop/Features/TD-4624-Addregexvalidationofprofessionalregistrationnumberfieldthroughouttheapplication
TD-4624 Add regex validation of professional registration number field throughout the application
2 parents afffbc6 + 89780e5 commit b97887c

File tree

5 files changed

+66
-27
lines changed

5 files changed

+66
-27
lines changed

DigitalLearningSolutions.Web.Tests/Helpers/ProfessionalRegistrationNumberHelperTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ public void ValidateProfessionalRegistrationNumber_sets_error_when_hasPrn_is_not
107107

108108
[TestCase(null, ErrorMessagesTestHelper.MissingNumberError)]
109109
[TestCase("", ErrorMessagesTestHelper.MissingNumberError)]
110-
[TestCase("1234", ErrorMessagesTestHelper.LengthError)]
111-
[TestCase("1234", ErrorMessagesTestHelper.LengthError)]
112110
[TestCase("01234_", ErrorMessagesTestHelper.InvalidFormatError)]
113111
[TestCase("01234 ", ErrorMessagesTestHelper.InvalidFormatError)]
114112
[TestCase("01234$", ErrorMessagesTestHelper.InvalidFormatError)]

DigitalLearningSolutions.Web.Tests/TestHelpers/ErrorMessagesTestHelper.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@ namespace DigitalLearningSolutions.Web.Tests.TestHelpers
44
public static class ErrorMessagesTestHelper
55
{
66
public const string InvalidFormatError =
7-
"Invalid professional registration number format. " +
8-
"Valid formats include: 7 digits (e.g., 1234567), 1–2 letters followed by 6 digits (e.g., AB123456), " +
9-
"4–8 digits, an optional 'P' plus 5–6 digits, 'C' or 'P' plus 6 digits, " +
10-
"an optional letter plus 5–6 digits, 'L' plus 4–6 digits, " +
11-
"or 2 digits followed by a hyphen and 4–5 alphanumeric characters (e.g., 12-AB123).";
7+
@"The format you entered isn’t recognised. Please check and try again.
8+
<br>Valid formats include:
9+
<ul>
10+
<li>7 digits - example, 1234567</li>
11+
<li>1–2 letters followed by 6 digits - example, AB123456</li>
12+
<li>‘P’ followed by 5–6 digits - example, P12345, P123456</li>
13+
<li>‘C’ or ‘P’ followed by 6 digits - example, C123456, P123456</li>
14+
<li>Optional letter followed by 5–6 digits - example, A12345, B123456</li>
15+
<li>‘L’ followed by 4–6 digits - example, L1234, L123456</li>
16+
<li>2 digits, hyphen, then 4–5 alphanumeric characters - example, 12-AB123</li>
17+
</ul>";
1218

1319
public const string MissingNumberError = "Enter a professional registration number";
14-
public const string LengthError = "Professional registration number must be between 5 and 20 characters";
20+
public const string LengthError = "Professional registration number must be between 4 and 8 characters";
1521

1622
}
1723
}

DigitalLearningSolutions.Web/Helpers/ProfessionalRegistrationNumberHelper.cs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
namespace DigitalLearningSolutions.Web.Helpers
22
{
3+
34
using Microsoft.AspNetCore.Mvc.ModelBinding;
5+
using System.Text;
46
using System.Text.RegularExpressions;
57

68
public class ProfessionalRegistrationNumberHelper
@@ -39,28 +41,29 @@ public static void ValidateProfessionalRegistrationNumber(
3941
modelState.AddModelError("ProfessionalRegistrationNumber", "Enter a professional registration number");
4042
return;
4143
}
42-
43-
if (prn.Length < 5 || prn.Length > 20)
44-
{
45-
modelState.AddModelError(
46-
"ProfessionalRegistrationNumber",
47-
"Professional registration number must be between 5 and 20 characters"
48-
);
49-
}
50-
51-
const string pattern = @"^(\d{7}|[A-Za-z]{1,2}\d{6}|\d{4,8}|P?\d{5,6}|[C|P]\d{6}|[A-Za-z]?\d{5,6}|L\d{4,6}|\d{2}-[A-Za-z\d]{4,5})$";
44+
const string pattern = @"^(\d{7}|[A-Za-z]{1,2}\d{6}|\d{4,8}|P?\d{5,6}|[C|P]\d{6}|[A-Za-z]?\d{5,6}|L\d{4,6}|\d{2}-[A-Za-z\d]{4,5})$";
5245
var rg = new Regex(pattern, RegexOptions.IgnoreCase);
5346
if (!rg.Match(prn).Success)
5447
{
5548
modelState.AddModelError(
5649
"ProfessionalRegistrationNumber",
57-
"Invalid professional registration number format. " +
58-
"Valid formats include: 7 digits (e.g., 1234567), 1–2 letters followed by 6 digits (e.g., AB123456), " +
59-
"4–8 digits, an optional 'P' plus 5–6 digits, 'C' or 'P' plus 6 digits, " +
60-
"an optional letter plus 5–6 digits, 'L' plus 4–6 digits, " +
61-
"or 2 digits followed by a hyphen and 4–5 alphanumeric characters (e.g., 12-AB123)."
50+
GetProfessionalRegistrationNumberErrorMessage()
6251
);
6352
}
6453
}
54+
public static string GetProfessionalRegistrationNumberErrorMessage()
55+
{
56+
return @"The format you entered isn’t recognised. Please check and try again.
57+
<br>Valid formats include:
58+
<ul>
59+
<li>7 digits - example, 1234567</li>
60+
<li>1–2 letters followed by 6 digits - example, AB123456</li>
61+
<li>‘P’ followed by 5–6 digits - example, P12345, P123456</li>
62+
<li>‘C’ or ‘P’ followed by 6 digits - example, C123456, P123456</li>
63+
<li>Optional letter followed by 5–6 digits - example, A12345, B123456</li>
64+
<li>‘L’ followed by 4–6 digits - example, L1234, L123456</li>
65+
<li>2 digits, hyphen, then 4–5 alphanumeric characters - example, 12-AB123</li>
66+
</ul>";
67+
}
6568
}
6669
}

DigitalLearningSolutions.Web/Views/Shared/_EditRegistrationNumber.cshtml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
var subject = Model.IsSelfRegistrationOrEdit ? "you" : "they";
1919
var capitalisedSubject = Model.IsSelfRegistrationOrEdit ? "You" : "They";
20+
var professionalRegistrationNumberError = ViewData.ModelState["ProfessionalRegistrationNumber"]?.Errors.FirstOrDefault()?.ErrorMessage;
21+
if (ViewData.ModelState.ContainsKey("ProfessionalRegistrationNumber"))
22+
{
23+
ViewData.ModelState["ProfessionalRegistrationNumber"].Errors.Clear();
24+
}
2025
}
2126

2227
<input type="hidden" asp-for="IsSelfRegistrationOrEdit" />
@@ -53,7 +58,7 @@
5358
No
5459
</label>
5560
</div>
56-
61+
5762
<div class="nhsuk-radios__item">
5863
<input name="@nameof(Model.HasProfessionalRegistrationNumber)"
5964
class="nhsuk-radios__input"
@@ -66,9 +71,16 @@
6671
Yes
6772
</label>
6873
</div>
69-
<div class="nhsuk-radios__conditional @(optionYesSelected ? " " : " nhsuk-radios__conditional--hidden") @(professionalRegNumberErrorHasOccurred ? "form-group-wrapper--error" : "" )"
74+
<div class="nhsuk-radios__conditional"
7075
id="@professionalRegConditionalId">
71-
<vc:text-input asp-for="@nameof(Model.ProfessionalRegistrationNumber)"
76+
@if (professionalRegistrationNumberError != null)
77+
{
78+
<span class="nhsuk-error-message" id="ProfessionalRegistrationNumber-error">
79+
<span class="nhsuk-u-visually-hidden">Error:</span>
80+
@Html.Raw(professionalRegistrationNumberError)
81+
</span>
82+
}
83+
<vc:text-input asp-for="@nameof(Model.ProfessionalRegistrationNumber)"
7284
label="Professional Registration Number"
7385
populate-with-current-value="true"
7486
type="text"

DigitalLearningSolutions.Web/Views/TrackingSystem/Delegates/EditDelegate/Index.cshtml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
<div class="nhsuk-grid-column-full">
1616
@if (errorHasOccurred)
1717
{
18-
<vc:error-summary order-of-property-names="@(new[] {
18+
var professionalRegistrationNumberError = ViewData.ModelState["ProfessionalRegistrationNumber"]?.Errors.FirstOrDefault();
19+
if (professionalRegistrationNumberError == null)
20+
{
21+
<vc:error-summary order-of-property-names="@(new[] {
1922
nameof(Model.FirstName),
2023
nameof(Model.LastName),
2124
nameof(Model.CentreSpecificEmail),
@@ -28,6 +31,23 @@
2831
nameof(Model.Answer4),
2932
nameof(Model.Answer5),
3033
nameof(Model.Answer6) })" />
34+
}
35+
if (professionalRegistrationNumberError != null)
36+
{
37+
<div class="nhsuk-error-summary" aria-labelledby="error-summary-title" role="alert" tabindex="-1">
38+
<h2 class="nhsuk-error-summary__title" id="error-summary-title">
39+
There is a problem
40+
</h2>
41+
<div class="nhsuk-error-summary__body">
42+
<span class="nhsuk-error-message" id="ProfessionalRegistrationNumber-error">
43+
<span class="nhsuk-u-visually-hidden">Error:</span> <span class="nhsuk-error-message" id="ProfessionalRegistrationNumber-error">
44+
<span class="nhsuk-u-visually-hidden">Error:</span>
45+
@Html.Raw(professionalRegistrationNumberError.ErrorMessage)
46+
</span>
47+
</span>
48+
</div>
49+
</div>
50+
}
3151
}
3252

3353
<h1 class="nhsuk-heading-xl">Edit delegate details</h1>

0 commit comments

Comments
 (0)