Brian Dunning's FileMaker Custom Functions

LuhnCheckDigit ( SeedNumber )

Creates a Luhn ( Mod 10 ) check digit

  Average rating: 4.2 (37 votes) Log in to vote

Tim Griffith   Tim Griffith

Share on Facebook Share on Twitter

  Sample input:
LuhnCheckDigit (1234567890)
  Sample output:
7

  Function definition: (Copy & paste into FileMaker's Edit Custom Function window)

Luhn Check Digit Creator

Author: Tim Griffith

Format: LuhnCheckDigit ( SeedNumber )

This will create a Luhn ( Mod 10 ) check digit for a string
of numbers up to 40 characters long.

Test it by inputting the first 15 digits of a credit card
as the SeedNumber. This function will generate the last
number on that card.

Update: Errors noted and updated to observation ... Mod ( Length(SeedNumber) ; 2 ) - 1

 

Comments

Phil Hanson   Phil Hanson
May 30, 2010
I've tried this CF but it didn't test on credit cards or the banks test sheet as expected. Is this the same MOD10 as specified below?

MOD10V01 (W01M101F3) (STANDARD LUHNS MODULUS 10)
Weights Array 01 02 01 02 01 02 01 02 01 02 01 02 01 02 01 02 01 02 01 02
Start Left N Add Digits Y
Divide By 10 Keep Zero Y
Subtract From 10 CD Length 1
 
Thomas Seidler   Thomas Seidler, London
Sep 28, 2010
likewise found it did not work...

after due investigation you need to change one line in code:

at final case statement you have "Mod ( SeedNumber ; 2 ) > 0" (odd/even detector). Needs to be:

Mod ( Length(SeedNumber) ; 2 ) - 1

T
 
Tim Griffith   Tim Griffith
Dec 21, 2010
Error fixed 12-21-2010

Thank you for noting so.

Sorry so long for update. I forgot my password until now.
 
Quentin King   Quentin King, Brighton
Aug 28, 2014
Can you help to adapt this for calculating a check digit for alphanumeric input?
 
Michael E. Kupietz, FileMaker Consultant   Michael E. Kupietz, FileMaker Consultant, San Francisco, CA
Jun 18, 2017
Great function, thanks!

With minor changes, I converted this to accept a whole CC number and simply return 1/0 for whether the Luhn algorithm validates it.

At the beginning, change:

Let ( [
Cmpr = Filter ( SeedNumber ; "0123456789" ) ;
.
.
.

to:

Let ( [
lastDigit = Right(SeedNumber;1);
SeedNumber=Left(SeedNumber;Length(SeedNumber)-1);
Cmpr = Filter ( SeedNumber ; "0123456789" ) ;
.
.
.

and at the end, change

.
.
.
Set_Even = A_Set_X + B_Set_1] ;

/* Noted update ...at final case statement you have "Mod ( SeedNumber ; 2 ) > 0" (odd/even detector). Needs to be:
Mod ( Length(SeedNumber) ; 2 ) - 1 ... */

Case (
Length ( SeedNumber ) > 40 ; "Source length limited to 40 characters" ;
Mod ( Length(SeedNumber) ; 2 ) - 1 ; Right ( Int ( 10 - ( Mod ( Set_Odd ; 10 ) ) ) ; 1 ) ;
Right ( Int ( 10 - ( Mod ( Set_Even ; 10 ) ) ) ; 1 )
)

)

to:

.
.
.
Set_Even = A_Set_X + B_Set_1;

digit = Case (
Length ( SeedNumber ) > 40 ; "Source length limited to 40 characters" ;
Mod ( Length(SeedNumber) ; 2 ) - 1 ; Right ( Int ( 10 - ( Mod ( Set_Odd ; 10 ) ) ) ; 1 ) ;
Right ( Int ( 10 - ( Mod ( Set_Even ; 10 ) ) ) ; 1 )
)

] ;

/* Noted update ...at final case statement you have "Mod ( SeedNumber ; 2 ) > 0" (odd/even detector). Needs to be:
Mod ( Length(SeedNumber) ; 2 ) - 1 ... */

If (digit=lastDigit;1;0)
)

And that's it.

This will make LuhnCheck(4111111111111111) = 1, indicating a valid CC# without any further comparison.
 
Michael E. Kupietz, FileMaker Consultant   Michael E. Kupietz, FileMaker Consultant, San Francisco, CA
Jun 18, 2017
Ack! Somehow an extra space got added... chance "cm pr" in my revision of the top few lines to "cmpr".
 

Log in to post comments.

 

Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.

Under construction. Email me your wish list for improvements.