Created
June 15, 2018 12:53
-
-
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))
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
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
It works for me only if I replace == to != in line 25.