|
|
Question : I am trying to validate a credit card number on a cfform
|
|
|
Answer : I am trying to validate a credit card number on a cfform
|
|
Especiailly with something like credit card numbers you should also validate server side - not just in javascript.
Here is what I use :
Variable#" = false> tNumber) EQ 13) OR (Len(ATTRIBUTES.AccountNumber) EQ 16))> er) NEQ 16> er) NEQ 15> er) NEQ 16> checkSumTotal = 0; digitSum = 0; multiplier = 1; for (digitCounter = #Len(ATTRIBUTES.AccountNumber)#; digitCounter GT 0; digitCounter = digitCounter - 1) { // get digit and double if necessary digitSum = multiplier * Val(Mid(#ATTRIBUTES.AccountNumber#, digitCounter, 1)); // mathematic trick to get sum of the two digits if number is 10, 12, 14, 16, or 18 if (digitSum GT 9) { digitSum = digitSum - 9; } // add current digit totals to checkSum Total checkSumTotal = checkSumTotal + digitSum; // set multiplier for next digit (i.e. mathematic trick to flip between 2 and 1) multiplier = 3 - multiplier; } // mark the card as invalid if it did not pass the Luhn Mod-10 Test if ((checkSumTotal mod 10) NEQ 0) { ValidCard = false; } Variable#" = ValidCard>
I removed the other account types we also allow (we have other account types besides Credit card numbers like Departmental Accounts, etc. Account types are 3 digit numbers and we make sure that the account type for credit cards start with a 0.
For example:
Visa : 001 Mastercard : 002 American Express : 003 Discover: 004
Departmental Accounts : 200
Note this is not the account number - just the account type.
You could easily add other types to the above such as Corporate accounts, student accounts, etc. just by adding a new case to the switch....
|
|
|
|
|