About This Code
Brief Description:
Credit Card Validation with @Formula
Contributor:
Steven Chapman
Notes Version:
R4.x, R5.x, R6.x
Last Modified:
24 Mar 2003
OpenNTF Disclaimer
All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.
Code / Description
REM "CCard Validation";
REM "Steven Chapman - Mohegan Sun Casino, Uncasville CT";
REM "Date Created: 01/23/2003";
REM "This formula utilizes the LUHN Formula to validate Credit Card Number formats only";
REM "This formula does not ensure the validity of all Credit Card Numbers, merely that the number";
REM " follows the correct Credit Card number algorithim for the given card";
REM " ' Visa: 430-00000-00000";
REM " ' American Express: 372-00000-00000";
REM " ' Mastercard: 521-00000-00000";
REM " ' Discover: 620-00000-00000";
REM "Computed for Display Field Formula- Determine if Credit card number is in correct format";
REM "Fields used in this formula";
REM "FIELD: CardType TYPE: Keywords USE: Card Type CHOICES: Visa|V; American Express|AX; Master Card|MC; Discover Card|D";
REM "FIELD: CC TYPE: Text Use: Card Number";
REM "FIELD: Current field, any name TYPE: Text, Computed for Display USE: Display Credit Card Format Verification OR message";
REM "If CardType hasn't been choosen, Leave";
@If(@Length(CardType)<1;@Return("SELECT CARD TYPE"); @Success);
REM "If Card Number hasn't been entered, Leave";
@If(@Length(CC)<1; @Return("Enter Card Number"); @Success);
lstCards:="V":"AX":"MC":"D";
lstCardFmt:="4":"37":"5":"6";
REM "Strip characters '.' and '-'";
vClean:=@ReplaceSubstring(CC; ".":"-"; "-":"");
REM "Pad left of number to be 16 characters.";
vF16:=@If(@Length(vClean) < 16; @Repeat("0"; 16-@Length(vClean)) + vClean; vClean);
nNum:=@TextToNumber(vF16);
vListLoc:=@Member(CardType; lstCards);
vFmtNum:=@Subset(@Subset(lstCardFmt; vListLoc); -1);
REM "If Beginning is invalid for Card Type, Leave";
@If(@Left(@Text(nNum); @Length(vFmtNum))=vFmtNum; @Success;@Return("- Invalid Card -"));
REM "Set temp variables to Odd digits * 2";
v1a:=@TextToNumber(@Left(vF16; 1)) * 2;
v3a:=@TextToNumber(@Right(@Left(vF16; 3); 1)) * 2;
v5a:=@TextToNumber(@Right(@Left(vF16; 5); 1)) * 2;
v7a:=@TextToNumber(@Right(@Left(vF16; 7); 1)) * 2;
v9a:=@TextToNumber(@Right(@Left(vF16; 9); 1)) * 2;
v11a:=@TextToNumber(@Right(@Left(vF16; 11); 1)) * 2;
v13a:=@TextToNumber(@Right(@Left(vF16; 13); 1)) * 2;
v15a:=@TextToNumber(@Right(@Left(vF16; 15); 1)) * 2;
REM "For Odd digits - if Digit > 9, subtract 9)";
v1:=@If(@Length(@Text(v1a))>1; v1a-9;v1a);
v3:=@If(@Length(@Text(v3a))>1; v3a-9;v3a);
v5:=@If(@Length(@Text(v5a))>1; v5a-9;v5a);
v7:=@If(@Length(@Text(v7a))>1; v7a-9;v7a);
v9:=@If(@Length(@Text(v9a))>1; v9a-9;v9a);
v11:=@If(@Length(@Text(v11a))>1; v11a-9;v11a);
v13:=@If(@Length(@Text(v13a))>1; v13a-9;v13a);
v15:=@If(@Length(@Text(v15a))>1; v15a-9;v15a);
REM "Set temp variables to Even digits";
v2:=@TextToNumber(@Right(@Left(vF16; 2); 1));
v4:=@TextToNumber(@Right(@Left(vF16; 4); 1));
v6:=@TextToNumber(@Right(@Left(vF16; 6); 1));
v8:=@TextToNumber(@Right(@Left(vF16; 8); 1));
v10:=@TextToNumber(@Right(@Left(vF16; 10); 1));
v12:=@TextToNumber(@Right(@Left(vF16; 12); 1));
v14:=@TextToNumber(@Right(@Left(vF16; 14); 1));
v16:=@TextToNumber(@Right(vF16; 1));
@If(@Modulo(@Sum(v1:v2:v3:v4:v5:v6:v7:v8:v9:v10:v11:v12:v13:v14:v15:v16); 10)=0; "- Card Format Verified -"; "- Invalid Card -")
Usage / Example
This code is for a Computed For Display field, and requires the following fields:
CardType, CC (credit card #)
See remarks in the code for additional information and usage.