Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Validate Estonian national identification number (isikukood).
/*
* Validate Estonian national identification code.
*
* Copyright (c) 2009 Mika Tuupola
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*/
function isikukood(kood) {
var multiplier_1 = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 1);
var multiplier_2 = new Array(3, 4, 5, 6, 7, 8, 9, 1, 2, 3);
var control = kood.charAt(10);
var retval = false;
var mod = 0;
var total = 0;
/* Do first run. */
for (i=0; i < 10; i++) {
total += kood.charAt(i) * multiplier_1[i];
}
mod = total % 11;
/* If modulus is ten we need second run. */
total = 0;
if (10 == mod) {
for (i=0; i < 10; i++) {
total += kood.charAt(i) * multiplier_2[i];
}
mod = total % 11;
/* If modulus is still ten revert to 0. */
if (10 == mod) {
mod = 0;
}
}
return control == mod;
}
@tuupola
Copy link
Author

tuupola commented Mar 18, 2020

I can find information only in Estonian. Below is a quick translation of the Wikipedia page.

-cut-

The control number is calculated using mod 11 using either multiplier set 1 or 2.

Multiplier set 1: 1 2 3 4 5 6 7 8 9 1
Multiplier set 2: 3 4 5 6 7 8 9 1 2 3

Take the first 10 numbers of the id code and multiply them with a number from set 1. Divide the sum of multiplying results with 11. If remainder is not 10 this is the control number.

If however reminder is 10 take the first 10 numbers of the id code and multiply them with a number from set 2. Divide the sum of multiplying results with 11. If remainder is not 10 this is the control number. If however reminder is 10 control number will be 0.

-cut-

You could also check dknight/Isikukood-js.

@abilogos
Copy link

abilogos commented Mar 20, 2020

Hi tuupola i have ported this gist in php. in This address:
php version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment