Mod10V01 check digit

I need to calculate a Mod10V01 check digit on one of my fields. I have the freely available code below. How can I apply this code below to append the check digit to my “Num” field?

/*
 * JavaScript implementation of the Luhn algorithm, with calculation and validation functions
 */

/* luhn_checksum
 * Implement the Luhn algorithm to calculate the Luhn check digit.
 * Return the check digit.
 */
function luhn_checksum(code) {
    var len = code.length
    var parity = len % 2
    var sum = 0
    for (var i = len-1; i >= 0; i--) {
        var d = parseInt(code.charAt(i))
        if (i % 2 == parity) { d *= 2 }
        if (d > 9) { d -= 9 }
        sum += d
    }
    return sum % 10
}

/* luhn_caclulate
 * Return a full code (including check digit), from the specified partial code (without check digit).
 */
function luhn_caclulate(partcode) {
    var checksum = luhn_checksum(partcode + "0")
    return checksum == 0 ? 0 : 10 - checksum
}

You could call the below functions anywhere (data mapper, template, print presets).

The function luhnCheckDigitCalculate(code) takes a numeric string as paratemeter and returns the Luhn Check Digit:

function luhnCheckDigitCalculate(code) {
	code+="0";
    var codeLength = code.length;
    var codeLengthParity = codeLength % 2;
    var luhnChecksum = 0;
    var luhnCheckDigit;
    for (var i = codeLength-1; i >= 0; i--) {
        var luhnDigit = parseInt(code.charAt(i));
        if (i % 2 == codeLengthParity){luhnDigit *= 2;}
        if (luhnDigit > 9){luhnDigit -= 9;}
        luhnChecksum += luhnDigit;
    }
    luhnCheckDigit = luhnChecksum %10;
    return (luhnCheckDigit == 0) ? 0 : 10 - luhnCheckDigit;
}

The below luhnFullCodeCalculate(code) uses the above luhnCheckDigitCalculate(code) function to return the full code including the Luhn Check Digit:

function luhnFullCodeCalculate(code){
    return code.toString() + luhnCheckDigitCalculate(code).toString();
}

Finally, if you need to check whether a numeric string including its Luhn check digit is valid, then call the below luhnValidate(fullCode) function:

function luhnValidate(fullCode){
    return luhnCheckDigitCalculate(fullCode) == 0;
}

So now, imagine you have a CSV file where Num is your code and you need to assign return it with its Luhn Check Digit:

  • simply add a JavaScript based Extract field
  • call the above function(s)
luhnFullCodeCalculate(data.extract('Num',0));

function luhnFullCodeCalculate(code){
    return code.toString() + luhnCheckDigitCalculate(code).toString();
}

function luhnCheckDigitCalculate(code) {
	code+="0";
    var codeLength = code.length;
    var codeLengthParity = codeLength % 2;
    var luhnChecksum = 0;
    var luhnCheckDigit;
    for (var i = codeLength-1; i >= 0; i--) {
        var luhnDigit = parseInt(code.charAt(i));
        if (i % 2 == codeLengthParity){luhnDigit *= 2;}
        if (luhnDigit > 9){luhnDigit -= 9;}
        luhnChecksum += luhnDigit;
    }
    luhnCheckDigit = luhnChecksum %10;
    return (luhnCheckDigit == 0) ? 0 : 10 - luhnCheckDigit;
}

Thanks Rod. I tried adding a new javascript extract field, however my input file isn’t a csv, but a multi-line pre formatted data file, so I need to extract on position. I’m not sure how to make this work in this instance

You could first extract the field based on Location. Then change the extraction Mode on that field to JavaScript, you will then get the JavaScript expression in the format similar to:

data.extract(7,23,6,1,"<br />");

http://help.objectiflune.com/en/planetpress-connect-user-guide/1.6/#datamapper/API/Method_extract.htm

You could add the trim() function to remove leading and trailing spaces or even assign it to a variable:

var tempCode = data.extract(7,23,6,1,"<br />").trim();

Next, use it in the above script:

luhnFullCodeCalculate(tempCode);

That worked perfectly, thanks for your help Rod

Is the Luhn check digit calculation the same as the EAN/UCC-13 check digit calculation?

I don’t think so. You may want to ask this question in a new post.