Skip to content

Instantly share code, notes, and snippets.

@lonalore
Created June 15, 2018 12:53
Show Gist options
  • Save lonalore/d81503844ea11776ae5a9eb5c600cc38 to your computer and use it in GitHub Desktop.
Save lonalore/d81503844ea11776ae5a9eb5c600cc38 to your computer and use it in GitHub Desktop.
TAJ szám validálás (Validation function for hungarian Social Security Number (TAJ))
<?php
/**
* Validation function for hungarian Social Security Number (TAJ).
*
* @param string $ssn
* The hungarian Social Security Number (TAJ).
*
* @return bool
* TRUE, if the number is valid. Otherwise FALSE.
*/
function validate($ssn) {
$ssn = preg_replace("/[^0-9]/", "", $ssn);
$length = strlen($ssn);
if ($length !== 9) {
return FALSE;
}
$sum = 0;
$num = '';
for ($i = 0; $i < $length; $i++) {
if ($i < 8) {
$sum += (($i % 2 == 0) ? $ssn[$i] * 7 : $ssn[$i] * 3);
$num .= $ssn[$i];
}
}
$num .= $sum % 10; // CDV
return $ssn === $num;
}
@Syriuss
Copy link

Syriuss commented Nov 5, 2018

It works for me only if I replace == to != in line 25.

@distvan
Copy link

distvan commented Apr 18, 2020

The CDV computation method is not correct in the line 30. The right one would be the following: $num .= ($i % 10 == 0) ? 0 : 10 - ($sum % 10);

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