The German tax ID ( Steueridentifikationsnummer ) has the following properties:
- It has 11 digits.
- The first digit cannot be 0
- In the first ten digits: one number occurs exactly twice or thrice, one or two digits appear zero time, and the remaining digits are displayed exactly once.
- The last digit is the checksum Code example for the checksum
The third point of view is a bit complicated for me in an elegant style. I already have code for the other three items, but I would like to get input for the latter, so this may be a small small link for other people.
$taxNumber = $_POST['taxNumber'];
echo preg_replace("/[^0-9]/", "", $taxNumber);
if (strlen($taxNumber != 11)) {
$taxNumberValid = false;
} else if ($taxNumber[0] == "0") {
$taxNumberValid = false;
} else {
$numbers = str_split($taxNumber);
$sum = 0;
$product = 10;
for ($i = 0; $i <= 9; $i++) {
$sum = ($numbers[$i] + $product) % 10;
if ($sum == 0) {
$sum = 10;
}
$product = ($sum * 2) % 11;
}
$checksum = 11 - $product;
if ($checksum == 10) {
$checksum = 0;
}
if ($taxNumber[10] != $checksum) {
$taxNumberValid = false;
}
}