The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 18, 2019, 11:08:12 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: validateCreditCard() (validateCC.js)  (Read 3994 times)
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« on: June 22, 2007, 05:54:43 PM »

This little function does a pretty thorough check on a credit card number to see if it is valid. NOTE: This does not go up against a merchant or anything, it simply looks to see if the number passes the standard Checksum10 test  and has the right structure for the type of card i is presenting to be. Note that this is in a standalone file, validateCC.js.

If it is valid, the function returns the type of card ie., VI/MC/AX/DI as well as DC for Diners, CB for CarteBlanche, ER for EnRoute (international) and JCB (Japanese). Currently, our system only accepts VI/MC/AX and DI, although I think we will accept DC in the future. For what it's worth. Note that spaces, dashes and periods in the number do not matter - they are removed during analysis.

Code:
ccType = validateCreditCard('4111-1111-1111-1111'); // returns VI
ccType = validateCreditCard('4111-1111-1111-1112'); // returns boolean FALSE
ccType = validateCreditCard('4111111111111111'); returns VI
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: August 22, 2008, 05:14:23 PM »

What a MAROON. Posted this ages ago but forgot to post the actual javascript. Found this today when looking for the code. Here it is, finally  D'oh!

Code:
function validateCreditCard(workStr)
{
workStr = workStr.replace(/\.|\-| /g, '');

// if the string is not 13..16 digits exactly it is not valid
if (!workStr.match(/^[0-9]{13,16}$/)) { return false; }

// Split the string into parts for analysis...
var chars = new Array();
var len = workStr.length;
for (var i=len-1; i>=0; i--) { chars[(len - 1) - i] = workStr.charAt(i); }

var checkVal = 0;
for (var i=0; i<len; i++)
{
// If this is an odd position in the string then I simply add the value
// at <that> position to the total... if it is an even position then I
// multiply it * 2 and add it to the total - if the amount is > 10 then
// I add the 2 digits together for example: 2*7 is 14, but the final
// value is 5 because it's 1 + 4. Also, this is not zero indexed - I count
// the first digit to be, literally the oneth, so even though it's index
// is zero I consider it to be an odd position. Also, as you may have
// noticed from the previous little routine this all takes place against
// the input value backward. Jeebers!
if ((i + 1) % 2) { checkVal += (chars[i] - 0); }
else {
// the answer is false, aka zero, so this is an even number
var thisVal = chars[i] * 2;
if (thisVal <= 9) { checkVal += (thisVal - 0); }
if (thisVal >= 10) { checkVal += (thisVal - 9); }
}
}

// So: if the resulting value I just created mod 10 is not zero, it's not a valid CC...
if (checkVal % 10) { return false; }

var chars1 = workStr.charAt(0);
var chars2 = workStr.substr(0, 2);
var chars3 = workStr.substr(0, 3);
var chars4 = workStr.substr(0, 4);

if ( (chars1 == '4') && ((len == 16) || (len == 13)) ) { return 'VI'; } // Visa
if ( (len == 16) && (chars2 >= '51') && (chars2 <= '55')) { return 'MC'; } // MasterCard
if ( (len == 15) && ((chars2 == '34') || (chars2 == '37')) ) { return 'AX'; } // AmEx
if ( (chars4 == '6011') && (len == 16) ) { return 'DI'; } // Discover
if ( (((chars3 >= '300') && (chars3 <= 305)) || (chars2 == '36')) && (len == 14) ) { return 'DC'; } // Diners Club
if ( (chars2 == '38') && (len == 14) ) { return 'CB'; } // Carte Blanche
if ( ((chars4 == '2014') || (chars4 == '2149')) && (len == 15) ) { return 'ER'; } // En Route
if ( ((chars4 == '2131') || (chars4 == '1800')) && (len == 15) ) { return 'JC'; } // JCB
if ( (chars1 == '3') && (len == 16) ) { return 'JC'; } // JCB Also
return false;
}
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #2 on: August 22, 2008, 05:19:18 PM »

lol

it would probably be a good idea to add in a switch for denying the common test numbers as well.
https://www.paypal.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm

Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #3 on: August 22, 2008, 09:40:52 PM »

You could, but that would be up to an app designer IMO - I use this and allow test numbers to pass, then check them at the server so that I can confirm the mechanics of my sites. And I'll check for test numbers at the server in any case, since the likes of you are lurking about the tubes... Wink
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #4 on: August 22, 2008, 11:01:54 PM »

considering i find carts constantly that allow me to checkout with a test card...

all it takes is one dumb ass who isnt paying attention, for me to get free toys.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #5 on: August 23, 2008, 11:02:39 AM »





ROFLMAO

Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #6 on: March 31, 2009, 12:33:07 PM »

(Note: This topic was accidentally misplaced and so folks may not have seen it.)
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!