Last active
January 24, 2022 19:16
-
-
Save fhferreira/3adc422e40bc31a39679 to your computer and use it in GitHub Desktop.
Credit Card Validation
This file contains hidden or 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
Elo: | |
/^((((636368)|(438935)|(504175)|(451416)|(636297))\d{0,10})|((5067)|(4576)|(4011))\d{0,12})$/ | |
Hipercard: | |
/^(606282\d{10}(\d{3})?)|(3841\d{15})$/ | |
----------------------------------------- | |
| Bandeira | Comeca com | Máximo de número | Máximo de número cvc | | |
| ---------- | ------------------------------------------- | ---------------- | -------------------- | | |
| Visa | 4 | 13,16 | 3 | | |
| Mastercard | 5 | 16 | 3 | | |
| Diners | 301,305,36,38 | 14,16 | 3 | | |
| Elo | 636368, 438935, 504175, 451416, | | | | |
| | 36297, 5067,4576,4011 | 16 | 3 | | |
| Amex | 34,37 | 15 | 4 | | |
| Discover | 6011,622,64,65 | 16 | 4 | | |
| Aura | 50 | 16 | 3 | | |
| jcb | 35 | 16 | 3 | | |
| Hipercard | 38,60 | 13,16,19 | 3 | | |
----------------------------- | |
Bin de cartões de crédito para validação | |
https://gist.github.com/erikhenrique/5931368 |
This file contains hidden or 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
(function(window) { | |
var gateway = window.gateway || {}; | |
window.gateway = gateway; | |
function normalizeCardNumber(cardNumber) { | |
// converts it's value to a string | |
cardNumber += ''; | |
return cardNumber.replace(/[\s+|\.|\-]/g, ''); | |
} | |
function CreditCard() { | |
if ( !( this instanceof CreditCard ) ) { | |
return new CreditCard(); | |
} | |
} | |
CreditCard.prototype = { | |
isValid: function(creditCardNumber) { | |
var cardType = CreditCard.prototype.cardType(creditCardNumber); | |
creditCardNumber = normalizeCardNumber(creditCardNumber); | |
if (!cardType){ | |
return false; | |
} else if (cardType.brand === "HIPERCARD") { | |
return true; // There's no validation for hipercard. | |
} else { | |
// Luhn algorithm: http://en.wikipedia.org/wiki/Luhn_algorithm | |
var checksum = 0; | |
for (var i=(2-(creditCardNumber.length % 2)); i<=creditCardNumber.length; i+=2) { | |
checksum += parseInt(creditCardNumber.charAt(i-1), 10); | |
} | |
// Analyze odd digits in even length strings or even digits in odd length strings. | |
for (i=(creditCardNumber.length % 2) + 1; i<creditCardNumber.length; i+=2) { | |
var digit = parseInt(creditCardNumber.charAt(i-1), 10) * 2; | |
if (digit < 10) { checksum += digit; } else { checksum += (digit-9); } | |
} | |
if ((checksum % 10) === 0) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
}, | |
cardType: function(creditCardNumber, loose) { | |
var brand, result, | |
brands = { | |
VISA: /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/, | |
MASTERCARD: /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/, | |
AMEX: /^3[4,7]\d{13}$/, | |
DINERS: /^3[0,6,8]\d{12}$/, | |
HIPERCARD: /^(606282\d{10}(\d{3})?)|(3841\d{15})$/ | |
}, | |
// for non-strict detections | |
looseBrands = { | |
VISA: /^4\d{3}-?\d{2}/, | |
MASTERCARD: /^5[1-5]\d{2}-?\d{2}/, | |
AMEX: /^3[4,7]\d{2}/, | |
DINERS: /^3(?:0[0-5]|[68][0-9])+/, | |
HIPERCARD: /^606282|3841\d{2}/ | |
}; | |
creditCardNumber = normalizeCardNumber(creditCardNumber); | |
if (loose) { | |
brands = looseBrands; | |
} | |
for (brand in brands) { | |
if (brands.hasOwnProperty(brand) && | |
creditCardNumber.match(brands[brand])) { | |
result = { brand : brand }; | |
} | |
} | |
return result; | |
}, | |
isSecurityCodeValid: function(creditCardNumber, csc) { | |
var type = gateway.creditCard.cardType(creditCardNumber), | |
digits; | |
digits = (type.brand === "AMEX") ? 4 : 3; | |
var regExp = new RegExp('[0-9]{' + digits + '}'); | |
return (csc.length === digits && regExp.test(csc)); | |
}, | |
isExpiryDateValid: function(month, year) { | |
month = parseInt(month, 10); | |
year = parseInt(year, 10); | |
if(month < 1 || month > 12) { | |
return false; | |
} | |
if((year+'').length !== 2 && (year+'').length !== 4) { | |
return false; | |
} | |
if((year+'').length === 2) { | |
if(year > 80) { | |
year = "19" + year; | |
} else { | |
year = "20" + year; | |
} | |
} | |
if(year < 1000 || year >= 3000) { | |
return false; | |
} | |
return !CreditCard.prototype.isExpiredDate(month, year); | |
}, | |
isExpiredDate: function(month, year) { | |
var now = new Date(); | |
var thisMonth = ("0" + (now.getMonth() + 1)).slice(-2); | |
var thisYear = now.getFullYear(); | |
month = ("0" + (month)).slice(-2); | |
if(year.toString().length === 2) { | |
if(year > 80) { | |
return true; | |
} else { | |
year = "20" + year; | |
} | |
} | |
var currentDate = thisYear + thisMonth; | |
var customerDate = year + month; | |
return parseInt(customerDate, 10) < parseInt(currentDate, 10); | |
} | |
}; | |
gateway.creditCard = CreditCard(); | |
})(window); |
This file contains hidden or 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
gateway.creditCard.isValid("4111111111111111"); //return true | |
gateway.creditCard.isValid("4111 1111-1111.1111"); //return true | |
gateway.creditCard.isValid("1919191919191919"); //return false | |
gateway.creditCard.isValid("41111"); //return false | |
var valid = gateway.creditCard.isValid("4111111111111111"); | |
print("Valid? "+valid); | |
valid = gateway.creditCard.isValid("4111 1111 1111 1111"); | |
print("Valid? "+valid); | |
valid = gateway.creditCard.isValid("4111.1111.1111.1111"); | |
print("Valid? "+valid); | |
valid = gateway.creditCard.isValid("4111-1111-1111-1111"); | |
print("Valid? "+valid); | |
var cscValid = gateway.creditCard.isSecurityCodeValid("12345"); | |
print("CSC Valid? "+cscValid); | |
var type = gateway.creditCard.cardType("5105105105105100"); | |
print("Type? "+type["brand"]); | |
type = gateway.creditCard.cardType("4111111111111111"); | |
print("Type? "+type["brand"]); | |
type = gateway.creditCard.cardType("4111 1111 1111 1111"); | |
print("Type? "+type["brand"]); | |
type = gateway.creditCard.cardType("341111111111111"); | |
print("Type? "+type["brand"]); | |
var now = new Date(); | |
var isExpiryDateValid = gateway.creditCard.isExpiryDateValid(now.getMonth()+1+"", now.getYear()+1900+""); | |
isExpiryDateValid = gateway.creditCard.isExpiryDateValid("10", "2010"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment