Created
June 1, 2011 10:50
-
-
Save DimitarChristoff/1002106 to your computer and use it in GitHub Desktop.
mootools creditcard class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.CreditCard = new Class({ | |
implements: [Options, Events], | |
options: { | |
//credit cards you accept | |
CARDS: [ | |
{ | |
Visa: /^4([0-9]){12}(?:[0-9]{3})?$/ | |
}, | |
{ | |
MasterCard: /^5[1-5][0-9]{14}$/ | |
}, | |
{ | |
Discover: /^6(?:011|5[0-9]{2})[0-9]{12}$/ | |
}, | |
{ | |
AmericanExpress: /^3[47][0-9]{13}$/ | |
}, | |
{ | |
DinersClub: /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/ | |
}, | |
{ | |
JCB: /^(?:2131|1800|35\d{3})\d{11}$/ | |
}, | |
{ | |
Maestro: /^((67\d{2})|(4\d{3})|(5[1-6]\d{2})|(6011))-?\d{4}-?\d{4}-?\d{4}|3[4,7]\d{13}$/ | |
} | |
], | |
ut: "Unknown Type", | |
testCards: [ | |
"4929000000006", // visa cc | |
"5404000000000001", // mc | |
"4462000000000003", // visa delta/debit | |
"5641820000000005", // maestro | |
"4917300000000008" // electron | |
] | |
}, | |
initialize: function(number) { | |
//if not number then return false | |
if (!number) { | |
return false; | |
} | |
//the given number is automatically stripped of whitespace/ | |
this.number = number.replace(/\s/g, '').trim(); | |
//for every card from CARDS create a boolean | |
//method to tell us is exists that type or not | |
this.options.CARDS.each(function(obj, index) { | |
this._addHandler(index); | |
}, this); | |
}, | |
isValid: function() { | |
return this._verifyLuhn(); | |
}, | |
_verifyLuhn: function() { | |
var number = this.number; | |
var sum = 0, | |
alt = false, | |
i = number.length - 1, | |
num; | |
if (number.length < 13 || number.length > 19) { | |
return false; | |
} //end if | |
while (i >= 0) { | |
//get the next digit | |
num = parseInt(number.charAt(i), 10); | |
//if it`s not a valid number then abort | |
if (isNaN(num)) { | |
return false; | |
} | |
//if it`s an alternate number then double | |
if (alt) { | |
num *= 2; | |
if (num > 9) { | |
num = (num % 10) + 1; | |
} //endif | |
} //end if | |
//flip the alternate bit | |
alt = !alt; | |
//add to the rest of the sum | |
sum += num; | |
//go to the next digit | |
i--; | |
} //end while | |
return (sum % 10 == 0); | |
}, | |
getType: function() { | |
//first, test whether the card is valid | |
//if valid then | |
if (this._verifyLuhn()) { | |
//loop through the object this.options.CARDS | |
var cardType = false; | |
this.options.CARDS.some(function(obj) { | |
//if one of them then return the card | |
var card = Object.keys(obj)[0]; | |
if (this['is' + card]()) { | |
// save card type | |
cardType = card; | |
// exit loop | |
return true; | |
} | |
}, this); | |
// return type found or unknwn type | |
return cardType || this.options.ut; | |
//otherwise the card isn't valid | |
} else { | |
return "The card is not valid!"; | |
} | |
}, | |
_addHandler: function(index) { | |
var obj = this.options.CARDS[index], card = Object.keys(obj)[0], cardRegEx = Object.values(obj)[0]; | |
return this['is' + card] = function() { | |
return cardRegEx.test(this.number); | |
}; | |
} | |
}); //end class CreditCard |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment