LiveCode for FM Manual Validation and CheckingHow do I check if a UK NI number is in the correct format?

How do I check if a UK NI number is in the correct format?

In this lesson we will see how you check the format of a UK National Insurance number.

The format of a UK National Insurance number is two prefix letters, six digits, and one suffix letter. For example AB123456C.

There are a number of rules to determine the validity of a NI number

  • Neither of the first two letters can be D, F, I, Q, U or V
  • The second letter also  cannot be O
  • After the two prefix letters there are 6 digits
  • The suffix letter is either A, B, C, or D.

Regular Expressions

A regular expression(regex) is a special text string for describing a search pattern. They are very useful for checking pieces of text that have a standand format, such as email addresses, phone number etc.

LiveCode allows you to use a regular expression to check the validity of a piece of text should conform to a standard format.

The matchText function

You can use the LiveCode matchText function to check whether a string contains a specified pattern. The function returns true if the pattern matches the string, and false if not.

The NI regular expression

We need to build a regular expression that matches a valid NI number. We take the conditions described above and turn it into a regular expression

1. A UK National Insurance number begins with two prefix letters. Neither of the first two letters can be D, F, I, Q, U or V and the second letter cannot be O.

This means the first number starts with 2 letters. The first must be one of A, B, C, E, G, H, J, K, L, M, N, O, P, R, S, T, W, X, Y or Z. The second must be one A, B, C, E, G, H, J, K, L, M, N, P, R, S, T, W, X, Y or Z.

In a regex we describe this as

[A-CEGHJ-PR-TW-Z]{1}[A-CEGHJ-NPR-TW-Z]{1}

This means

exactly one letter {1} matching a characters in the list of allowed characters [A-CEGHJ-PR-TW-Z] followed by exactly one letter {1} matching a character in the list of allowed characters [A-CEGHJ-NPR-TW-Z].

2. The two prefix letters are followed by 6 digits, 0-9.

In a regex we describe this as

[0-9]{6}

3. After the 6 digits there is one suffix letter, either A, B, C or D.

In a regex we describe this as

[A-D]{1}

We put these together, in order, to get

[A-CEGHJ-PR-TW-Z]{1}[A-CEGHJ-NPR-TW-Z]{1}[0-9]{6}[A-D]{1}

Finally we need to ensure that the NI number matches the regex exactly, with no extra characters at the start or the end. This is because the matchText function checks whether a string contains the specfied pattern anywhere within it, but we want to ensure the whole string matches the pattern exactly.

We start the regex with ^, which matches the starting position within the string. Meaning the pattern matching the regex must start at the start of the string.

We end the regex with $, which matches the ending position within the string. Meaning the pattern matching the regex must end at the end of the string.

These 2 charaters together at the start and end of the regex ensure the string must match the regex exactly.

The final regular expression is

^[A-CEGHJ-PR-TW-Z]{1}[A-CEGHJ-NPR-TW-Z]{1}[0-9]{6}[A-D]{1}$

Validating a National Insurance Field in FileMaker

In the FileMaker solution we have a field called named 'niNumber'. The value is valid if the matches the regex we worked out above.

Once you have created the field click the `Options...` button indicated in the screenshot with a 1 marker.

Validation Calculation

Go to the Validation tab and check the 'Validated by calculation' checkbox. Now click 'Specify' to the enter a calculation to validate the field.

Using LCEval

Our validation step is only one line of LiveCode that returns a value so we will use LCEval to run the check.

Any newlines entered into the FileMaker `Specify Calculation` dialog are removed so when using LCEval here we need to use LiveCode's newline synonym `;` for each command. Any quotes in the script need to be escaped with FileMaker's escape character `\`

Enter the validation script into the `Specify Calculation` dialog as follows and click OK.

LCEval("
   matchText(fmParam(1), \"^[A-CEGHJ-PR-TW-Z]{1}[A-CEGHJ-NPR-TW-Z]{1}[0-9]{6}[A-D]{1}$\")
"; 
niNumber)

 

Invalid Entry Message

Entering a custom invalid entry message is a good idea to ensure users don't become frustrated if they don't know the form of a valid entry.

On the `Validation` tab of the `Field Options` dialog check the `Display custom message if validation fails` checkbox and enter a message describing the required form of a valid entry.

Testing Validation

Test the field validation with valid and invalid entries to ensure the validation failure dialog is presented appropriately.

A valid NI number - AB123456C

An invalid NI number - QQ123456C

Other uses

You can use this method to validate any type of text that conforms to a pattern that can be described by a regular expression.

Some examples are

  • US Social Security numbers
  • Credit Card numbers
  • Dates required in a particular format
  • Postcode or ZIP code
  • Email address

 

0 Comments

Add your comment

E-Mail me when someone replies to this comment