PHP verifies German tax ID (Steueridentifikationsnummer)

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.

# validate tax number
        $taxNumber = $_POST['taxNumber'];
        echo preg_replace("/[^0-9]/", "", $taxNumber);

        if (strlen($taxNumber != 11)) {
            # 11 digits
            $taxNumberValid = false;
        } else if ($taxNumber[0] == "0") {
            # first digit != 0
            $taxNumberValid = false; 
        } else {
            # one digit two times, one digit zero times


            # checksum
            $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;
            }
        }
+4
2

:

// remove whitespaces, slashes & other unnecessary characters
$taxNumber = preg_replace("/[^0-9]/", "", $taxNumber);

// by default the taxnumber is correct
$taxNumberValid = true;

// taxnumber has to have exactly 11 digits
if (strlen($taxNumber) != 11) {
    $taxNumberValid = false;                
}

// first digit cannot be 0
if ($taxNumber[0] == "0") {
    $taxNumberValid = false; 
} 

/* 
 make sure that within the first ten digits:
     1.) one digit appears exactly twice or thrice
     2.) one or two digits appear zero times
     3.) and oll other digits appear exactly once once
*/
$digits = str_split($taxNumber);
$first10Digits = $digits;
array_pop($first10Digits);
$countDigits = array_count_values ($first10Digits);
if (count($countDigits) != 9 && count($countDigits) != 8) {
    $taxNumberValid = false;
}

// last check: 11th digit has to be the correct checkums
// see http://de.wikipedia.org/wiki/Steueridentifikationsnummer#Aufbau_der_Identifikationsnummer
$sum = 0;
$product = 10;
for($i = 0; $i <= 9; $i++) {
    $sum = ($digits[$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;
}

2017

2016 , .

2017 , , .

+2

JS, @Pascal Klein:

function countValues(arr) {
  return arr.reduce((obj, item) => {
    obj[item] = obj[item] ? ++obj[item] : 1;
    return obj;
  }, {});
}

function validateTIN(tin) {
  const tinLength = 11;
  
  // Taxnumber has to have exactly 11 digits.
  if (tin.length !== tinLength) {
    return false;
  }
  
  // First digit cannot be 0.
  if (tin[0] === '0') {
    return false;
  }
  
  /* 
   make sure that within the first ten digits:
     1.) one digit appears exactly twice or thrice
     2.) one or two digits appear zero times
     3.) and all other digits appear exactly once
  */
  const tinArray = tin.split('').slice(0, -1);
  const valueCount = countValues(tinArray);
  const valueCountLength = Object.keys(valueCount).length;

  if (valueCountLength !== 8 && valueCountLength !== 9) {
    return false;
  }
  
  // 11th digit has to match the checkum.
  let sum = 0;
  let product = 10;
  
  for(let i = 0; i < tinLength - 1; i++) {
    sum = (+tinArray[i] + product) % 10;
     if (sum === 0) {
         sum = 10;
     }
    product = (sum * 2) % 11;
  }

  let checksum = 11 - product;
  
  if (checksum === 10) {
    checksum = 0;
  }

  if (+tin[tinLength - 1] !== checksum) {
    return false;
  }
  
  return true;
}

const tin1 = 'gbg';
const tin2 = '42344677908';
const tin3 = '12005078909';
const tin4 = '36574261809'; // valid
const tin5 = '10863924976'; // valid

console.log(tin1, validateTIN(tin1));
console.log(tin2, validateTIN(tin2));
console.log(tin3, validateTIN(tin3));
console.log(tin4, validateTIN(tin4));
console.log(tin5, validateTIN(tin5));
Hide result
0

Source: https://habr.com/ru/post/1545051/


All Articles