VerifyMod10_len
Back to ListDescription: Verify Credit card numbers, etc, luhn, modulo 10
FileMaker Prototype:
Set Variable [$res; ACF_Run("VerifyMod10_len"; string_number; int_correctlength)]
Category: MATH
Function source:
function VerifyMod10_len ( string number, int correctlength )
array int digits;
int i, sum, digx;
// remove dashes and non-numeric characters
number = regex_replace ( "[^\d]", number, "");
// Correct length
int len = length ( number );
if ((correctlength == 0 || len != correctlength) &&
(correctlength != 0 || len < 2)) then
return false;
end if
// get the last control digit
string ctrl = right ( number,1);
// remove the control digit
len --;
number = substring ( number, 0,len);
// Build an array of integers.
for (i=1, len )
digits[] = ascii ( substring ( number, i-1, 1 ) ) - 48 ;
end for
// Back traverse every second and double it. If > 9, subtract 9.
for ( i = len, 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
for (i=1, len )
sum += digits[i];
end for
// sum multiplied by 9, modulo 10 gives the digit.
digx = mod ( sum*9 , 10);
// check if digit matches.
return (ctrl == string(digx)); // True on match, otherwise false.
end
The VerifyMod10_len function can be used to verify credit card numbers and similar codes. To verify a credit card number, pass the number along with the correct length (typically 16 for credit cards). The function returns true or falsebased on whether the number has the correct length and if the check digit matches.
If the CorrectLength parameter is zero (0), we dont check the length, only that it is at least two digits for the function to work (one control digit, and 1 digit number).
Example
Set Variable [$CreditCardOK; ACF_Run("VerifyMod10_len"; "1234-5678-9123-4563"; 16)]
- OR -
Set Variable [$CreditCardOK; ACF_Run("VerifyMod10_len"; "1234567891234563"; 16)]
This returns true because "3" is the correct check digit for this number, and it contains the correct number of digits (16).
If the number contains dashes, they are removed before checking the length.
For cases where the length may vary?
If the CorrectLength parameter is zero (0), we dont check the length, only that it is at least two digits for the function to work (one control digit, and 1 digit number).
Example - Both the lines below return true, because 8 is correct for 18, and 5 is correct for 123455. Any other last digit returns false.
Set Variable [$luhnOK; ACF_Run("VerifyMod10_len"; "18"; 0)]
Set Variable [$luhnOK; ACF_Run("VerifyMod10_len"; "123455"; 0)] 