Lasso Soft Inc. > Home

[jina_checksum]

Linkjina_checksum
AuthorJolle Carlestam
CategoryCustom Tag
Version8.5.x
LicensePublic Domain
Posted11 Mar 2009
Updated13 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.
Usage with optional param -add
jina_checksum('NNNNNNNN', -add) returns the correct checksum for the checked value.

var('checkme' = '4124 9720 0139 655');
jina_checksum($checkme, -add);
-> 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,
	-opt = 'add'
);

// 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
// usage with optional param -add jina_checksum('NNNNNNNN', -add)
// returns the correct checksum for the checked value

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

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

	if(local_defined('add'));
		((#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;

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

/define_tag;

Related Tags

Comments

No comments

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft