-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindex.html
160 lines (128 loc) · 5.25 KB
/
index.html
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
160
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Generate SA ID Numbers</title>
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script>
// calculates the Lohn digit, see https://en.wikipedia.org/wiki/Luhn_algorithm
function calculateCheckDigit(digitsAsString) {
//const digits = ('' + dob + gender + sequence + citizenship + eight).split('').map(d => Number(d))
const digits = digitsAsString.replace(/\D/g, '').split('').map(d => Number(d))
const checkSum = digits.reverse().map((d, ix) => {
if (ix % 2 === 0) {
d *= 2
if (d > 9) {
d -= 9
}
}
return d
}).reduce((memo, d) => memo += d, 0)
return checkSum * 9 % 10
}
// show a valid ID number generated using the values in the form
function showIdNumber() {
const form = document.forms.f1
const values = [
form.year, form.month, form.day,
form.gender,
{ value: form.sequence.value || '896' /* sequence */ },
{ value: form.cship.value || '0' /* citizenship */ },
{ value: '8' /* a */ },
]
withoutCheckDigit = values.map(e => e.value).join('')
const idNumber = withoutCheckDigit + calculateCheckDigit(withoutCheckDigit)
document.getElementById('result').innerHTML = `<p>${ idNumber }</p>`
}
function showExpertOptions() {
$('#showExpertOptions').toggle()
$('#expertOptions').toggle()
}
</script>
</head>
<body>
<div class="container">
<h3>Generate (Fake) South-African ID Numbers</h1>
<div class="row">
<div class="col-xs-12">
<form id="f1" name="f1" onsubmit="showIdNumber(); return false">
<div class="input-group col-xs-12">
<label for="year">Year of Birth</label>
<select class="form-control" id="year" name="year"></select>
</div>
<div class="input-group col-xs-12">
<label for="month">Month of Birth</label>
<select class="form-control" id="month" name="month"></select>
</div>
<div class="input-group col-xs-12">
<label for="day">Day of Birth</label>
<select class="form-control" id="day" name="day"></select>
</div>
<div class="input-group col-xs-12">
<input type="radio" name="gender" id="female" value="4" checked="checked" />
<label for="female">Female</label>
<br/>
<input type="radio" name="gender" id="male" value="5" />
<label for="male">Male</label>
</div>
<div id="showExpertOptions">
<a href="#" onclick="showExpertOptions()">Show expert options</a>
</div>
<div style="display: none;" id="expertOptions">
<div class="input-group col-xs-12">
<label for="sequence">Sequence</label>
<select class="form-control" id="sequence" name="sequence"></select>
</div>
<div class="input-group col-xs-12">
<input type="radio" name="cship" id="citizen" value="0" checked="checked" />
<label for="citizen">Citizen</label>
<br/>
<input type="radio" name="cship" id="resident" value="1" />
<label for="resident">Resident</label>
</div>
</div>
<input class="btn btn-primary" type="submit" />
</form>
<h3>Result</h3>
<div id="result">Hint: submit the form to get an ID number.</div>
<h3>More Information</h3>
<p>See <a href="https://en.wikipedia.org/wiki/National_identification_number#South_Africa">Wikipedia</a> or
<a href="http://knowles.co.za/generating-south-african-id-numbers/">this article</a>.
</p>
<p>
Read <a href="https://github.com/Chris927/generate-sa-idnumbers">the code</a>.
</p>
</div>
</div>
<script>
// add 'option' elements to the 'select' elements (for year, month, day)
function addOptions(id, from, to, toLabel, toValue, defaultValue) {
const selectElement = document.getElementById(id)
const values = []
for (let i = 0; i <= to - from; i++) values[i] = i + from
const options = values
.map(v => ((v < 10 ? '0' : '') + v))
.map(v => {
const value = toValue ? toValue(v) : v
const isDefault = defaultValue && defaultValue === value
const label = toLabel ? toLabel(v) : v
return `<option value=${ value } ${ isDefault ? 'selected="selected"' : '' }>${ label }</option>`
})
selectElement.innerHTML = options.join()
}
const year = new Date().getYear() // need the current year to display age... 2016 yields 116 (thanks to funny JS)
const pad = function(n) {
const str = "" + n
const pad = "000"
return pad.substring(0, pad.length - str.length) + str
}
addOptions('year', 20, 99, v => `${v} (~${year - Number(v)} years old)`)
addOptions('month', 1, 12)
addOptions('day', 1, 31)
addOptions('sequence', 0, 999, v => pad(v), pad, '800')
</script>
</body>
</html>