Skip to content

Instantly share code, notes, and snippets.

@abilogos
Last active Apr 13, 2020
Embed
What would you like to do?
Validate Estonian National code
<?php
/*
* Validate Estonian national identification code.
*
* Copyright (c) 2020 Ali Hakami
* port from @author Mika Tuupola (javascript)
* https://gist.github.com/tuupola/180321
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
* @param string $code estonian national code
* @return boolean is valid or not
*/
public function nationalCodeisValid($code){
//$multiplier_1 = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 1);
//$multiplier_2 = array(3, 4, 5, 6, 7, 8, 9, 1, 2, 3);
$control = intval(substr($code,-1));
$retval = false;
$mod = 0;
$total = 0;
/* Do first run. */
for ($i=-1; $i < 9; $i++) {
$total += intval($code[$i]) * ((($i+1)%9)+1);
}
$mod = $total % 11;
/* If modulus is ten we need second run. */
$total = 0;
if ($mod == 10) {
for ($i=1; $i < 11; $i++) {
$total += intval($code[$i]) * ((($i+1)%9)+1);
}
$mod = $total % 11;
/* If modulus is still ten revert to 0. */
if (10 == $mod) {
$mod = 0;
}
}
return $control == $mod;
}
@abilogos
Copy link
Author

abilogos commented Mar 20, 2020

ported from JavaScript:
https://gist.github.com/tuupola/180321

@abilogos
Copy link
Author

abilogos commented Mar 22, 2020

Thanks @vishalkvl.
I have Modified The Code as You Suggest.

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