South African ID number
A South African ID number is a 13-digit number which is defined by the following format: YYMMDDSSSSCAZ.
The first 6 digits (YYMMDD) are based on your date of birth. 20 February 1992 is displayed as 920220.
The next 4 digits (SSSS) are used to define your gender. Females are assigned numbers in the range 0000-4999 and males from 5000-9999.
The next digit (C) shows if you’re an SA citizen status with 0 denoting that you were born a SA citizen and 1 denoting that you’re a permanent resident.
The last digit (Z) is a checksum digit – used to check that the number sequence is accurate using a set formula called the Luhn algorithm.
<?php
namespace AppValidations;
trait SaIdNumberVerification
{
public function validateSouthAfricanId(string $idNumber): bool
{
// Check for valid length (13 digits) and numeric characters
if (strlen($idNumber) !== 13 || !is_numeric($idNumber)) {
return false;
}
// Extract components
$year = substr($idNumber, 0, 2);
$prefix = $this->determineCentury($year);
$birthYear = (int)$prefix . $year;
$BirthMonth = (int)substr($idNumber, 2, 2);
$BirthDay = (int)substr($idNumber, 4, 2);
$gender = substr($idNumber, 6, 4);
$citizenship = (int)substr($idNumber, 10, 1);
$checksum = (int)substr($idNumber, 12, 1);
// Validate birthdate (basic check)
if (!checkdate($BirthMonth, $BirthDay, $birthYear)) {
return false;
}
// Validate gender (0-4 female, 5-9 male)
// unnecessary check
if ($gender < '0000' || $gender > '9999') {
return false;
}
// Validate citizenship (0 - South African Citizen, 1 - Permanent Resident)
if ($citizenship !== 0 && $citizenship !== 1) {
return false;
}
// Calculate checksum digit using Luhn Algorithm
$digits = str_split(substr($idNumber, 0, 12));
$sum = array_reduce($digits, function ($carry, $digit) use (&$index) {
$digit = (($index++ % 2 === 0) ? $digit : $digit * 2);
return $carry + ($digit > 9 ? $digit - 9 : $digit);
}, 0);
$checkDigit = (10 - ($sum % 10)) % 10;
// Validate checksum
return $checksum === $checkDigit;
}
private function determineCentury($YY): string
{
return ($YY < date('y')) ? '20' : '19';
}
}
I tried the above and it works, open to suggestions or refactoring.
1