[jina_checksum]

Description

Link: [jina_checksum]
Author: Jolle Carlestam
Category: Custom Tag
Version: 8.5.x
License: Public Domain
Posted: Mar. 11, 2009
Updated: Mar. 13, 2009
More by this author...
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.

Parameters

none


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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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

none

Email:


Password:



Newest

Most Popular

Support tagSwap.net