ACF Library

AddMod11

Back to List

Description: Add Modulo 11 (Luhn Algorithm) control digit to a string of numbers

FileMaker Prototype:

Set Variable [$res; ACFU_AddMod11( string_number)]

Category: BOOTSTRAP

NOTE: The bootstrap library comes preloaded in the plugin. Read more

Function source:

/*

   Add Modulo 11 (Luhn Algorithm) control digit to a string of numbers
   This  one might also add "-" at the end, according to the spesifications. 
   
*/
function AddMod11 ( string number ) 
   FunctionID 236; 
   array int digits; 
   int l = length ( number ) ; 
   int i, mult; 
// Check only digits. 
   if ( regex_match ( "\d+", number ) ) then
   
   // Build an array of integers. 
      for (i=1, l ) 
         digits[] = ascii ( substring ( number, i-1, 1 ) ) - 48 ; 
      end for
      
   // Back traverse every one and multiply 
      mult = 2; 
      int sum; 
      for ( i = l, 1, -1 )
         sum += digits[i]*mult; 
         mult ++; 
         if ( mult > 7) then
            mult = 2; 
         end if
      end for
      
      print sum; 
   // difference 11 and mod 11 gives the digit. 
      int digx = 11 - mod ( sum , 11);
      string ctrl; 
      if ( digx > 9) then
         ctrl = "-"; 
      else
         ctrl = string ( digx ); 
      end if
   
      return number + ctrl; 
   else
      return "ERROR: Expects only digits as parameter: " + number; 
   end if
end 

The AddMod11 function adds a modulo-11 check digit to a string of numbers, using the Luhn algorithm.

Example

Set Variable [$res; ACFU_AddMod11("123456")]

The function returns "1234563", with the last digit as the check digit.

This can be used to generate or verify check digits for EAN codes, add validation to customer identification numbers on invoices, and more. ISBN numbers use this algorithm

A slightly modified version of this function can also be used to verify an existing check digit instead of adding one.

Back to List