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
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.