Using AI Copilot to add NI Number validation and tests to an existing VB Net Project

Using AI Copilot to add NI Number validation and tests to an existing VB Net Project

2 min read - 27 Feb, 2025aivisual studio

I have been asked to add a NI Number to one of our current VB .Net applications, and I though I would add some validation to the form, this seems like a great task for AI Copilot.

give me the vb code to validate a ni number

And it responded:

Public Function IsValidNINumber(niNumber As String) As Boolean
    ' Regular expression pattern for NI number
    Dim pattern As String = "^(?!BG|GB|KN|NK|NT|TN|ZZ)(?:[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z])\d{6}[A-D]$"
    Dim regex As New System.Text.RegularExpressions.Regex(pattern)

    ' Check if the NI number matches the pattern
    Return regex.IsMatch(niNumber)
End Function

I don't like trusting it blindly, so a quick check on google reveals:

 

What makes an NI number invalid?

  • The first two letters cannot be D, F, I, Q, U, or V.
  • The second letter cannot be O.
  • The prefixes BG, GB, NK, KN, TN, NT, and ZZ are not allocated.

I also asked Copilot:

now give me a list of test cases

Also ran this through a validator:

The AI event went as far as creating the Unit Tests:

 <Test>
 Public Sub IsValidNINumber_ValidNI_ReturnsTrue()
     Dim result As Boolean = frm.IsValidNINumber("AB123456C")
     Assert.IsTrue(result)
 End Sub

 <Test>
 Public Sub IsValidNINumber_InvalidNI_ReturnsFalse()
     Dim result As Boolean = frm.IsValidNINumber("123456AB")
     Assert.IsFalse(result)
 End Sub

 <Test>
 Public Sub IsValidNINumber_DisallowedPrefix_ReturnsFalse()
     Dim result As Boolean = frm.IsValidNINumber("BG123456C")
     Assert.IsFalse(result)
 End Sub

 <Test>
 Public Sub IsValidNINumber_MissingFinalLetter_ReturnsFalse()
     Dim result As Boolean = frm.IsValidNINumber("AB123456")
     Assert.IsFalse(result)
 End Sub

 <Test>
 Public Sub IsValidNINumber_ExtraCharacters_ReturnsFalse()
     Dim result As Boolean = frm.IsValidNINumber("AB123456CD")
     Assert.IsFalse(result)
 End Sub

And there it is, this post took way longer to write than it did to use AI to add code to my project. 

A quick blog post about AI helping me with NI validation using regular expressions
Published Thursday, 27 February 2025


Related Articles