Skip to content

Instantly share code, notes, and snippets.

@richsilv
Last active August 29, 2015 14:18
Show Gist options
  • Save richsilv/33acc8d9f1eccd146baa to your computer and use it in GitHub Desktop.
Save richsilv/33acc8d9f1eccd146baa to your computer and use it in GitHub Desktop.
National Insurance Number generator
function generateNINO(style) {
var group1 = '';
var group2 = '';
var group3 = '';
var group4 = '';
var group5 = '';
var randomArrayIndex = '';
var notAllowed = new Array('GB', 'BG', 'NK', 'KN', 'TN', 'NT', 'ZZ');
var firstAllowed = new Array('A', 'B', 'C', 'E', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'W', 'X', 'Y', 'Z');
var secondAllowed = new Array('A', 'B', 'C', 'E', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y', 'Z');
var lastAllowed = new Array('A', 'B', 'C', 'D', '');
var style = (style == null) ? 1 : style;
if (style < 1 || style > 2) {
style = 1;
}
while (group1 == '' || notAllowed.indexOf(group1) > -1) {
randomArrayIndex = Math.floor(Math.random() * firstAllowed.length);
group1 = firstAllowed[randomArrayIndex];
randomArrayIndex = Math.floor(Math.random() * secondAllowed.length);
group1 = group1 + secondAllowed[randomArrayIndex];
}
group2 = '' + Math.floor(Math.random() * 10) + Math.floor(Math.random() * 10);
group3 = '' + Math.floor(Math.random() * 10) + Math.floor(Math.random() * 10);
group4 = '' + Math.floor(Math.random() * 10) + Math.floor(Math.random() * 10);
randomArrayIndex = Math.floor(Math.random() * lastAllowed.length);
group5 = lastAllowed[randomArrayIndex];
if (style == 1) {
var final = group1 + group2 + group3 + group4 + group5;
} else {
var final = group1 + " " + group2 + " " + group3 + " " + group4;
if (group5 != '') {
final = final + " " + group5;
}
}
return final;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment