# How to Validate NI Number format

### #1 Russell

Russell
• Members
• Newbie
• 2 posts

Posted 23 March 2006 - 08:22 AM

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

### #2 redbullmarky

redbullmarky
• Staff Alumni
• 2,863 posts
• LocationBedfordshire, England

Posted 23 March 2006 - 09:32 AM

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

```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;

}
```

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

EDIT: Just had a playaround with it and alot of it can be taken out:

```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;
}
```

on the Preg_match line, please note the space in the [\ A-D] part
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users