-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathfngnino.js
159 lines (135 loc) · 2.93 KB
/
fngnino.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
FNG UK National Insurance Number Generator and Validator v1.2
Copyright © 2009 Fake Name Generator <http://www.fakenamegenerator.com/>
FNG UK National Insurance Number Generator and Validator v1.2 by the Fake Name
Generator is licensed to you under a Creative Commons Attribution-Share Alike
3.0 United States License.
For full license details, please visit:
http://www.fakenamegenerator.com/license.php
*/
// For more information on NINO, see:
// http://www.govtalk.gov.uk/gdsc/schemas/CitizenIdentificationTypes-v1-4.xsd
// Create a handy array function
Array.prototype.in_array = function ( obj ) {
var len = this.length;
for ( var x = 0 ; x <= len ; x++ ) {
if ( this[x] == obj ) return true;
}
return false;
}
// Setup our groups
var group1 = '';
var group2 = '';
var group3 = '';
var group4 = '';
var group5 = '';
var randomArrayIndex = '';
// Some combinations are not allowed
var notAllowed = new Array(
'GB',
'BG',
'NK',
'KN',
'TN',
'NT',
'ZZ'
);
// Some letters can't be first and some can't be second
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'
);
// Only certain characters can show up last
var lastAllowed = new Array(
'A',
'B',
'C',
'D',
''
);
function generateNINO( style ) {
// Set a default value for style and make sure it is valid
var style = (style == null) ? 1 : style;
if( style < 1 || style > 2 ) {
style = 1;
}
// Generate group1
while( group1 == '' || notAllowed.in_array(group1) ) {
// First letter
randomArrayIndex = Math.floor(Math.random() * firstAllowed.length);
group1 = firstAllowed[ randomArrayIndex ];
// Second letter
randomArrayIndex = Math.floor(Math.random() * secondAllowed.length);
group1 = group1 + secondAllowed[ randomArrayIndex ];
}
// Generate group2
group2 = ''+Math.floor(Math.random()*10)+Math.floor(Math.random()*10);
// Generate group3
group3 = ''+Math.floor(Math.random()*10)+Math.floor(Math.random()*10);
// Generate group4
group4 = ''+Math.floor(Math.random()*10)+Math.floor(Math.random()*10);
// Generate group5
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;
}
function validateNINO ( nino ) {
var regex = /^[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z] ?[0-9]{2} ?[0-9]{2} ?[0-9]{2} ?[ABCD]?/;
if (nino.match(regex)) {
regex = /^(GB)|^(BG)|^(NK)|^(KN)|^(TN)|^(NT)|^(ZZ)/;
if ( !nino.match(regex) ) {
return true;
}
}
return false;
}
/* Example usage */
// document.write( generateNINO(2) );
//document.write( validateNINO ('AA123456D') );