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.
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.
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.
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, " ", ""),
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.