Skip to content

Instantly share code, notes, and snippets.

@abilogos
Last active April 13, 2020 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abilogos/6399a5486d7911c582731b2b2224430d to your computer and use it in GitHub Desktop.
Save abilogos/6399a5486d7911c582731b2b2224430d to your computer and use it in GitHub Desktop.
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

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

@abilogos
Copy link
Author

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