ACF Library

AddMod10

Back to List

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

FileMaker Prototype:

Set Variable [$res; ACFU_AddMod10( string_number)]

Category: BOOTSTRAP

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

Function source:

/*

   Add Modulo 10 (Luhn Algorithm) control digit to a string of numbers
   
*/
function AddMod10 ( string number ) 
   FunctionID 235; 
   array int digits; 
   int l = length ( number ) ; 
   int i; 
// 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 second and double it. If > 9, subtract 9. 
      for ( i = l, 1, -2 )
         digits[i]=digits[i]*2; 
         if (digits[i]>9) then
            digits[i] = digits[i]-9; 
         end if
      end for
      
   // Sum of the digits
      int sum; 
      for (i=1, l ) 
         sum += digits[i]; 
      end for
      
   // sum multiplied by 9, modulo 10 gives the digit. 
      int digx = mod ( sum*9 , 10); 
   
      return number + digx; 
   else
      return "ERROR: Expects only digits as parameter: " + number; 
   end if
end 

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

Example

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

The function returns "1234566", 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, Credit card numbers and more.

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

Back to List