Javascript - Implementing Mod 10 validation (LUHN formula) for Credit Card numbers

If you have ever developed an application that accepts or processes Credit Card numbers, then LUHN's formula for Mod 10 validation of primary account numbers would come naturally to you. If you want to know the logic behind Mod 10 validation for Credit Card numbers, here's a very good reference.

I today implemented the LUHN's Mod 10 validation logic in javascript, and thought of sharing it. Here's a javascript method that accepts a string (of numbers) and returns true or false depending upon whether Mod 10 validation succeeds or not:

 

{syntaxhighlighter brush: jscript;fontsize: 100; first-line: 1; }var fnMod10 = function(number) { var doubled = []; for (var i = number.length - 2; i >= 0; i = i - 2) { doubled.push(2 * number[i]); } var total = 0; for (var i = ((number.length % 2) == 0 ? 1 : 0); i < number.length; i = i + 2) { total += parseInt(number[i]); } for (var i = 0; i < doubled.length; i++) { var num = doubled[i]; var digit; while (num != 0) { digit = num % 10; num = parseInt(num / 10); total += digit; } } if (total % 10 == 0) { return (true); } else { return (false); } }{/syntaxhighlighter}

Mod 10 validation essentially consists of doubling every second digit from right, and adding individual digits of the doubles to the reminaing digits in the original string and checking that the total is divisible by 10. For details, see this link as referenced above.

In the above code, we first collect all doubles for alternate digits starting from second right in an array, total the remaining digits in the original string, adding to this total the individual digits of each of the doubled figure and finally checking if the total is divisible by 10.

To make the topic of Credit Card validation a bit more complete, here's a method that checks the validity of Visa, Master Card and American Express credit card numbers in javascript. If all validations for a specific card type succeed, the method returns true, otherwise it returns a string describing the particular validation that failed.

 

{syntaxhighlighter brush: jscript;fontsize: 100; first-line: 1; }validateCreditCardNumber = function(ccnum) { var ccMsg = 'Credit Card Number should be of format: XXNNNNNNNNNNNNNNNN" where XX is the 2 character Credit Card code, i.e. VI for Visa, MC for MasterCard, and AX for American Express. For example VI4500000000000006. The minimum allowed length is 15.'; var ccFormat = 'XXNNNNNNNNNNNNNNNN'; if (ccnum.length < 15) { return (ccMsg); } var lowered = ccnum.toLowerCase(); var cctype = lowered.substr(0, 2); var match = cctype.match(/[a-zA-Z]{2}/); if (!match) { return (ccMsg); } var number = lowered.substr(2); match = number.match(/[^0-9]/); if (match) { return (ccMsg); } var fnMod10 = function(number) { var doubled = []; for (var i = number.length - 2; i >= 0; i = i - 2) { doubled.push(2 * number[i]); } var total = 0; for (var i = ((number.length % 2) == 0 ? 1 : 0); i < number.length; i = i + 2) { total += parseInt(number[i]); } for (var i = 0; i < doubled.length; i++) { var num = doubled[i]; var digit; while (num != 0) { digit = num % 10; num = parseInt(num / 10); total += digit; } } if (total % 10 == 0) { return (true); } else { return (false); } } switch (cctype) { case 'vi': case 'mc': case 'ax': //Mod 10 check if (!fnMod10(number)) { return ('Credit Card Number seems to be invalid.'); } break; } switch (cctype) { case 'vi': if (number[0] != '4' || (number.length != 13 && number.length != 16)) { return ('Visa Credit Cards should start with "4" and should be 13 or 16 numbers long.'); } break; case 'mc': if (number[0] != '5' || (number.length != 16)) { return ('MasterCard Credit Cards should start with "5" and should be 16 numbers long.'); } break; case 'ax': if (number[0] != '3' || (number.length != 15)) { return ('American Express Credit Cards should start with "3" and should be 15 numbers long.'); } break; default: return ('Card Type not recognized.'); break; } return (true); }{/syntaxhighlighter}

The function accepts a string where the first 2 characters are expected to be the prefix for the Credit Card provider (VI, MC or AX etc.), and the rest is expected to be a numeric string of atleast length 13.

Apart from Mod 10 validation, some additional card-type validations are also performed and a string describing the error (if any) is returned. In no error, true is returned.

You can enforce some more validations on the number depending upon card type (e.g. the second number for American Express cards can only be 4 or 7), but we already had extensive Credit Card validations server-side (including a direct hook-up to the card provider services for debits), and so were happy with some basic checks in javascript.

 

Web 2.0: 

Comments

where are my comments? and can you please answer my query. it would really help me alot in my project. thanks.

rahul's picture

Your comments have been deleted as they did not appear to be related to the blog post. See this:
http://www.rahulsingla.com/chit-chat/2010/07/general-help-and-support

Your are seeking help for general html/javascript issues and blog posts are not a place to do that.

im trying to use the codes you have provided. my question is what function should i delcare in order for my program to run. i'm looking for a "fuctionNameoffucntion()" kind of thing so that i can put it in my html codes but i dont see any. your codes are, from the eyes of an amateur programmer, very advanced. so im hoping you could somehow explain it to me. thank you very much.

rahul's picture

That is why I am suggesting not to seek help here, we are not discussing general html/javascript development. The short answer is javascript is not traditional programming language (like C/C++) where you create "programs" which run when clicked. Javascript is a scripting language where you define methods to respond to various types of events. I would again advise you to go for a basic html/javascript session before trying to use this code, w3schools.com should be a good start.

Exactly what I was looking for.

Hope you don't mind but I used this to create a knockout validation rule:
http://github.com/ericmbarnard/Knockout-Validation/wiki/User-Contributed...

Cheers

rahul's picture

Sure John, no problem at all...