Skip to content

Instantly share code, notes, and snippets.

@wurin7i
Last active March 1, 2020 15:44
Show Gist options
  • Save wurin7i/5657244 to your computer and use it in GitHub Desktop.
Save wurin7i/5657244 to your computer and use it in GitHub Desktop.
Damm validation & generation code in PHP.Damm algorithm is a check digit algorithm that detects all single-digit errors and all adjacent transposition errors.
<?php
/**
* The Damm check digit
* For more information cf. http://en.wikipedia.org/wiki/Damm_algorithm
* totally anti-symmetric quasigroup
*
* @author Wuri Nugrahadi <w.nugrahadi@gmail.com>
*/
if (! function_exists('taq'))
{
function taq($digits)
{
$taq_table = array(
'0317598642', '7092154863', '4206871359', '1750983426', '6123045978',
'3674209581', '5869720134', '8945362017', '9438617205', '2581436790');
$interim = 0;
foreach (str_split($digits) as $digit) {
if (! preg_match('~\d~', $digit))
{
return FALSE;
}
$interim = substr($taq_table[$interim], $digit, 1);
}
return $interim;
}
}
if (! function_exists('calc_check_digit'))
{
function calc_check_digit($digits)
{
return $digits.taq($digits);
}
}
if (! function_exists('is_check_digit_valid'))
{
function is_check_digit_valid($digits)
{
return taq($digits) == 0;
}
}
function taq(digits)
{
var taqTable = ['0317598642', '7092154863', '4206871359', '1750983426', '6123045978', '3674209581', '5869720134', '8945362017', '9438617205', '2581436790'];
var interim = 0;
var chars = digits.split('');
var index, len;
for (index = 0, len = chars.length; index < len; ++index) {
var digit = parseInt(chars[index]);
var taqRow = taqTable[interim];
var strInterim = taqRow.substr(digit, 1);
interim = parseInt(strInterim);
}
return interim;
}
console.log(taq('8436714657207786'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment