The Luhn algorithm or “mod 10” algorithm, is a checksum used to validate a variety of identification numbers including those used by most credit card vendors.
It was created by IBM scientist Hans Peter Luhn and described in U.S. Patent No. 2,950,048, filed on January 6, 1954, and granted on August 23, 1960. This is a pretty interesting algorithm as algorithms go and I would recommend checking it out on Wikipedia.
The following demonstrates how you can a CommonJS module implementation of this algorithm in your Titanium app.
Before we can get started, we need to generate a few fake credit card numbers for testing. For this demonstration I used the site “Get Credit Card Numbers”. This site provided us with the fake number we use in all of our examples.
Credit Card Helper CommonJS Module
First we create our module using the below code. This module has one function called verifyCardWithLuhn that has one parameter for you to pass in the credit card number you wish to validate. This function will return true if the provided credit card number is in the correct format, or false if the format is invalid.
exports.verifyCardWithLuhn=function(cardNumber){ //Remove spaces cardNumber = cardNumber.replace(/[^\d]/g, ""); //Grab our length for use later var cardLength = cardNumber.length; //If no length return invalid if (cardLength === 0){ return false; } //Get last digit var lastDigit = parseInt(cardNumber.substring((cardLength-1),cardLength),10); //Build string with all credit card digits minus the last one var cardNumberMinusLastDigit = cardNumber.substring(0,(cardLength-1)); //Build up our variables needed for our calculation var sum = 0, luhnLength = cardNumberMinusLastDigit.length, luhnKey = [0,1,2,3,4,-4,-3,-2,-1,0]; //Step 1 of our hash add the numbers together for (i=0; i<luhnLength; i++ ) { sum += parseInt(cardNumberMinusLastDigit.substring(i,i+1),10); } //Step 2 of our has, we now add in our key values for (i=luhnLength-1; i>=0; i-=2 ) { sum += luhnKey[parseInt(cardNumberMinusLastDigit.substring(i,i+1),10)] } //Adjust our sum as neeed var mod10 = sum % 10; mod10 = 10 - mod10; if (mod10===10) { mod10=0; } //Our hash should now mast the last digit of our number return (mod10===lastDigit); };
See the gist here.
Calling our Module
Calling our Credit Card Helper module is easy. We simply use the require method to add the CommonJS module into our project.
var my = {tools:{}}; //Import our module into our project my.tools.cardHelper = require('credit_card_helper'); //Our test credit card number that we got from http://www.getcreditcardnumbers.com/ var testCreditCardNumber ="49713268648235453"; //Call our verify method to check if our credit card number is in a correct format var ccCheck = my.tools.cardHelper.verifyCardWithLuhn(testCreditCardNumber); //alert the result back to the user alert(ccCheck);
See the gist here.
Things to reminder
Please remember this algorithm can only validate if the format is correct. You will still need to use a 3rd party service to determine if the number itself is active and valid.