Skip to content

Commit d5b19a4

Browse files
authored
Merge pull request #3355 from TechnologyEnhancedLearning/Develop/Features/TD-4624-Addregexvalidationofprofessionalregistrationnumberfieldthroughouttheapplication
TD-4624 Add regex validation of professional registration number field throughout the application
2 parents ef45c1a + 24d4146 commit d5b19a4

File tree

5 files changed

+36
-23
lines changed

5 files changed

+36
-23
lines changed

DigitalLearningSolutions.Web.Tests/Controllers/Register/RegisterDelegateByCentreControllerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public void LearnerInformationPost_updates_tempdata_correctly()
176176
const string answer4 = "answer4";
177177
const string answer5 = "answer5";
178178
const string answer6 = "answer6";
179-
const string professionalRegistrationNumber = "PRN1234";
179+
const string professionalRegistrationNumber = "PR123456";
180180
var model = new LearnerInformationViewModel
181181
{
182182
JobGroup = jobGroupId,

DigitalLearningSolutions.Web.Tests/Controllers/TrackingSystem/Delegates/EditDelegateControllerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public void Index_post_returns_view_with_model_error_with_invalid_prn()
175175
result.As<ViewResult>().Model.Should().BeOfType<EditDelegateViewModel>();
176176
AssertModelStateErrorIsExpected(
177177
result,
178-
"Invalid professional registration number format - Only alphanumeric characters (a-z, A-Z and 0-9) and hyphens (-) allowed"
178+
ErrorMessagesTestHelper.InvalidFormatError
179179
);
180180
A.CallTo(() => userService.GetDelegateById(A<int>._)).MustNotHaveHappened();
181181
}

DigitalLearningSolutions.Web.Tests/Helpers/ProfessionalRegistrationNumberHelperTests.cs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
namespace DigitalLearningSolutions.Web.Tests.Helpers
22
{
3-
using System.Linq;
43
using DigitalLearningSolutions.Web.Helpers;
4+
using DigitalLearningSolutions.Web.Tests.TestHelpers;
55
using FluentAssertions;
66
using FluentAssertions.Execution;
77
using Microsoft.AspNetCore.Mvc.ModelBinding;
88
using NUnit.Framework;
9+
using System.Linq;
910

1011
public class ProfessionalRegistrationNumberHelperTests
1112
{
@@ -70,7 +71,7 @@ public void ValidateProfessionalRegistrationNumber_does_not_set_errors_when_vali
7071
{
7172
// Given
7273
var state = new ModelStateDictionary();
73-
const string validPrn = "abc-123";
74+
const string validPrn = "AB123456";
7475

7576
// When
7677
ProfessionalRegistrationNumberHelper.ValidateProfessionalRegistrationNumber(
@@ -104,22 +105,13 @@ public void ValidateProfessionalRegistrationNumber_sets_error_when_hasPrn_is_not
104105
}
105106
}
106107

107-
[TestCase(null, "Enter a professional registration number")]
108-
[TestCase("", "Enter a professional registration number")]
109-
[TestCase("123", "Professional registration number must be between 5 and 20 characters")]
110-
[TestCase("0123456789-0123456789", "Professional registration number must be between 5 and 20 characters")]
111-
[TestCase(
112-
"01234_",
113-
"Invalid professional registration number format - Only alphanumeric characters (a-z, A-Z and 0-9) and hyphens (-) allowed"
114-
)]
115-
[TestCase(
116-
"01234 ",
117-
"Invalid professional registration number format - Only alphanumeric characters (a-z, A-Z and 0-9) and hyphens (-) allowed"
118-
)]
119-
[TestCase(
120-
"01234$",
121-
"Invalid professional registration number format - Only alphanumeric characters (a-z, A-Z and 0-9) and hyphens (-) allowed"
122-
)]
108+
[TestCase(null, ErrorMessagesTestHelper.MissingNumberError)]
109+
[TestCase("", ErrorMessagesTestHelper.MissingNumberError)]
110+
[TestCase("1234", ErrorMessagesTestHelper.LengthError)]
111+
[TestCase("1234", ErrorMessagesTestHelper.LengthError)]
112+
[TestCase("01234_", ErrorMessagesTestHelper.InvalidFormatError)]
113+
[TestCase("01234 ", ErrorMessagesTestHelper.InvalidFormatError)]
114+
[TestCase("01234$", ErrorMessagesTestHelper.InvalidFormatError)]
123115
public void ValidateProfessionalRegistrationNumber_sets_error_when_prn_is_invalid(
124116
string prn,
125117
string expectedError
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+

2+
namespace DigitalLearningSolutions.Web.Tests.TestHelpers
3+
{
4+
public static class ErrorMessagesTestHelper
5+
{
6+
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).";
12+
13+
public const string MissingNumberError = "Enter a professional registration number";
14+
public const string LengthError = "Professional registration number must be between 5 and 20 characters";
15+
16+
}
17+
}

DigitalLearningSolutions.Web/Helpers/ProfessionalRegistrationNumberHelper.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace DigitalLearningSolutions.Web.Helpers
22
{
3-
using System.Text.RegularExpressions;
43
using Microsoft.AspNetCore.Mvc.ModelBinding;
4+
using System.Text.RegularExpressions;
55

66
public class ProfessionalRegistrationNumberHelper
77
{
@@ -48,13 +48,17 @@ public static void ValidateProfessionalRegistrationNumber(
4848
);
4949
}
5050

51-
const string pattern = @"^[a-z\d-]+$";
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})$";
5252
var rg = new Regex(pattern, RegexOptions.IgnoreCase);
5353
if (!rg.Match(prn).Success)
5454
{
5555
modelState.AddModelError(
5656
"ProfessionalRegistrationNumber",
57-
"Invalid professional registration number format - Only alphanumeric characters (a-z, A-Z and 0-9) and hyphens (-) allowed"
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)."
5862
);
5963
}
6064
}

0 commit comments

Comments
 (0)