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
}
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: