Jump to content

How to Validate NI Number format


Russell

Recommended Posts

Any ideas as to how to validate UK National Insurance Numbers, they must all fall within these criteria:

## Must be 9 characters.
##First 2 characters must be alpha.
##Next 6 characters must be numeric.
##Final character can be A, B, C, D or space.
##First character must not be D,F,I,Q,U or V
##Second characters must not be D, F, I, O, Q, U or V.
##First 2 characters must not be combinations of GB, NK, TN or ZZ (the term combinations covers both GB and BG etc.)

Your help and assistance much appreciated
Link to comment
https://forums.phpfreaks.com/topic/5568-how-to-validate-ni-number-format/
Share on other sites

for the first 4 things ,you could do it with one check, then check the other bits seperately. make a function:

[code]
function check_ni($ni)
{
   $ni = strtoupper($ni);
   $not_first_two = array('D','F','I','Q','U','V');

   $is_ni = preg_match('/^[A-Z]{2}[0-9]{6}[\ A-D]{1}$/', $ni);

   if ($is_ni)
   {
      if (in_array($ni[0], $not_first_two) || in_array($ni[1], $not_first_two))
      {
          return false;
      }
          else
          {
              return true;
          }
   }
   else
      return false;

}
[/code]

only think it doesn't do is check the pairs for invalid pairings, but it should set you on your way.
cheers

[b]EDIT:[/b] Just had a playaround with it and alot of it can be taken out:

[code]
function check_ni($ni)
{
   $ni = strtoupper($ni);
  
   $is_ni = preg_match('/^[A-CEG-HJ-PR-TW-Z]{2}[0-9]{6}[\ A-D]{1}$/', $ni);

   if ($is_ni)
   {
      return true;
   }
   else
      return false;
}
[/code]

on the Preg_match line, please note the space in the [\ A-D] part

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.