summaryrefslogtreecommitdiff
path: root/README.md
blob: de3e46fe7164c26386851ed6b4a14232d0dfc6c3 (plain)
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
South African ID Number Parser
==============================

The ID Numbers issued in South Africa follow a regular format that you
can use to derive some information about them. The following
information is available:

* Date of Birth
* Sex
* Citizenship

This library can also check if the ID number supplied is a valid South
African ID number.

More information on the ID number format can be found
[here](http://geekswithblogs.net/willemf/archive/2005/10/30/58561.aspx)
and [here](http://knowles.co.za/generating-south-african-id-numbers/).

Usage
-----

### Parse Everything

The package exposes the `.parse(idNumber)` method for calling all of
the validation and parsing in one.

If validation fails, the resulting object only has the isValid property.

```
var saIdParser = require('south-african-id-parser');
var validIdNumber = '9001049818080';

var info = saIdParser.parse(validIdNumber);
//info === {
//  isValid: true,
//  dateOfBirth: new Date(1990, 01, 04),
//  isMale: true,
//  isFemale: false,
//  isSouthAfricanCitizen: true
//}

var invalidIdNumber = '1234567';
info = saIdParser.parse(invalidIdNumber);
//info === {
//  isValid: false
//}
```

### Only Validate

`.validate(idNumber)` only checks if the ID number is valid.

```
var saIdParser = require('south-african-id-parser');
var validIdNumber = '9001049818080';
var isValid = saIdParser.validate(validIdNumber);

//valid === true
```

### Only Parse Date of Birth

The method does not do a full validation on the ID number, but it will
return undefined if either the number supplied is not a 13 digit
number string or the date section of the ID number is invalid.

```
var saIdParser = require('south-african-id-parser');
var validIdNumber = '9001049818080';
var dateOfBirth = saIdParser.parseDateOfBirth(validIdNumber);

//dateOfBirth === new Date(1990, 01, 04)
```

The date of birth included in the ID number has a two digit year. For
example, 90 instead of 1990.

This is handled by comparing the date of birth to the current date,
and choosing the century that gives the person the lowest age, while
still putting their age in the past.

For example, assuming that the current date is 10 December 2015. If
the date of birth parsed is 10 December 15, it will be interpreted as
10 December 2015. If, on the other hand, the date of birth is parsed
as 11 December 15, that will be interpreted as 10 December 1915.

### Only Parse Sex

The method does not do a full validation on the ID number, but it will
return undefined if the number supplied is not a 13 digit number
string.

```
var saIdParser = require('south-african-id-parser');
var validIdNumber = '9001049818080';
var isFemale = saIdParser.parseIsFemale(validIdNumber);
var isMale = saIdParser.parseIsMale(validIdNumber);

//isFemale === false
//isMale === true
```

### Only Parse Citizenship

The method does not do a full validation on the ID number, but it will
return undefined if the number supplied is not a 13 digit number
string.

```
var saIdParser = require('south-african-id-parser');
var validIdNumber = '9001049818080';
var isSouthAfricanCitizen = saIdParser.parseIsSouthAfricanCitizen(validIdNumber);

//isSouthAfricanCitizen === true
```