Russell Posted March 23, 2006 Share Posted March 23, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/5568-how-to-validate-ni-number-format/ Share on other sites More sharing options...
redbullmarky Posted March 23, 2006 Share Posted March 23, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/5568-how-to-validate-ni-number-format/#findComment-19882 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.