Blog
Data - How to Validate UK National Insurance Numbers
July 5. 2023
Are you sure the National Insurance numbers you’re collecting are valid? This article walks through how to validate UK National Insurance numbers with Regex.
A question that a colleague recently asked me was - how do I validate a UK National Insurance number?
With the help of regular expressions, this task is relatively simple and this post walks through how to carry out this task.
With the help of online resources like the Regexlib website, we can find the following Regex to validate the number.
This formula will return true for the following example input. - JP123456C (note the Regex is case sensitive)
In keeping with the typical way of validating input, we can add a warning label and set the visible property like so:
We could also disable the save button on the screen by setting the DisplayMode property with formula like so.
With the help of regular expressions, this task is relatively simple and this post walks through how to carry out this task.
What's the format of a National Insurance number?
A UK National number is a number that looks like this - .“JP123456C”With the help of online resources like the Regexlib website, we can find the following Regex to validate the number.
"^[A-CEGHJ-PR-TW-Z]{1}[A-CEGHJ-NPR-TW-Z]{1}[0-9]{6}[A-DFM]{0,1}$"
How to validate a National Insurance number from Power Apps?
From Power Apps, we can verify that a user enters a valid National Insurance number by calling the IsMatch function in conjunction with the expression that we located above.The syntax we use looks like this.
IsMatch(txtNINumber.Text,
"^[A-CEGHJ-PR-TW-Z]{1}[A-CEGHJ-NPR-TW-Z]{1}[0-9]{6}[A-DFM]{0,1}$"
)
This formula will return true for the following example input. - JP123456C (note the Regex is case sensitive)
In keeping with the typical way of validating input, we can add a warning label and set the visible property like so:
We could also disable the save button on the screen by setting the DisplayMode property with formula like so.
How to validate a National Insurance number with spaces?
UK National number are often padded out with spaces like this - .“JP 12 34 56 C”
To validate the underlying number, we can adapt our formula by removing the spaces.
IsMatch(Substitute(txtNINumber.Text, " ", ""),
"^[A-CEGHJ-PR-TW-Z]{1}[A-CEGHJ-NPR-TW-Z]{1}[0-9]{6}[A-DFM]{0,1}$"
)
Conclusion
There may be scenarios where you need to validate an NI number. This post walked through the process of how to do this with a regular expression.
- Categories:
- validation
Previous
Related posts