This repository was archived by the owner on Mar 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
This repository was archived by the owner on Mar 4, 2025. It is now read-only.
Problem Validate NIE #14
Copy link
Copy link
Open
Description
We encountered some trouble to validate NIE. The problem come from replace Y, X and Z with 1, 0 and 2. With the current code, when the NIE ends by Z we replace it by 2 to call isValidNIF(). Next, this method calls getNIFCheckDigit() which return Z. Unfortunately, the last char at this moment is 2...
I propose this code for method isValidNIE() :
function isValidNIE($docNumber) {
$isValid = false;
$fixedDocNumber = "";
if (!preg_match("/^[A-Z]+$/i", substr($fixedDocNumber, 1, 1))) {
$fixedDocNumber = strtoupper(substr("000000000" . $docNumber, - 9));
} else {
$fixedDocNumber = strtoupper($docNumber);
}
if (isValidNIEFormat($fixedDocNumber)) {
if (substr($fixedDocNumber, 1, 1) == "T") {
$isValid = true;
} else {
$writtenDigit = substr($fixedDocNumber, - 1, 1);
/* The algorithm for validating the check digits of a NIE number is
identical to the altorithm for validating NIF numbers. We only have to
replace Y, X and Z with 1, 0 and 2 respectively; and then, run
the NIF altorithm */
$fixedDocNumber = str_replace('Y', '1', $fixedDocNumber);
$fixedDocNumber = str_replace('X', '0', $fixedDocNumber);
$fixedDocNumber = str_replace('Z', '2', $fixedDocNumber);
$writtenDigit2 = substr($fixedDocNumber, - 1, 1);
$correctDigit = getNIFCheckDigit($fixedDocNumber);
if ($writtenDigit == $correctDigit || $writtenDigit2 == $correctDigit) {
$isValid = true;
}
}
}
return $isValid;
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels