# [jina_checksum]

 Link jina_checksum Author Jolle Carlestam Category Custom Tag Version 8.5.x License Public Domain Posted 11 Mar 2009 Updated 13 Mar 2009 More by this author...

#### Description

Tag using the Luhn algorithm, a simple checksum formula, to handle checksums for a given numerical value. It can either validate that a number ends with a correct checksum or add a checksum to a number. The Luhn algorithm is used for credit card numbers, different ID numbers like in Sweden or Canada etc.

#### Sample Usage

jina_checksum('NNNNNNNN') will return true or false depending on if the checksum checks.
jina_checksum('NNNNNNNN', -add) returns the correct checksum for the checked value.

var('checkme' = '4124 9720 0139 655');
-> 8

#### Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

define_tag('checksum', -namespace = 'jina_',
-req = 'check', -copy,
);

// tag used for the Luhn algorithm a simple checksum formula
// used to validate a variety of identification numbers
// usage jina_checksum('NNNNNNNN')
// will return true or false depending on if the checksum checks
// returns the correct checksum for the checked value

local('temp',
'total' = integer,
'odd'
);

#check = string_replaceregexp(#check, -find = '[^0-9]', -replace = '');

((#check -> size % 2) == 1 ? #odd = true | #odd = false);
else;
((#check -> size % 2) == 0 ? #odd = true | #odd = false);
/if;

loop(-from = #check -> size, -to = 1, -by = -1);
if(#odd);
#temp = integer(#check -> get(loop_count)) * ((loop_count % 2) + 1);
else;
#temp = integer(#check -> get(loop_count)) * (((loop_count + 1) % 2) + 1);
/if;
(#temp > 9 ? #temp -= 9);
#total += #temp;
/loop;

return((#total % 10 == 0) ? 0 | ((#total + 10) - (#total % 10)) - #total);
else(#total % 10 == 0);
return(true);
else;
return(false);
/if;

/define_tag;