Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save franklinjavier/ab06e97e4fb0b1a2b56cbed077ba8028 to your computer and use it in GitHub Desktop.
Save franklinjavier/ab06e97e4fb0b1a2b56cbed077ba8028 to your computer and use it in GitHub Desktop.
função para validar cartão de crédito
//campo para validação da bandeira de cartão
function getCreditCardLabel(c){
// SOMENTE NÚMEROS
let re = new RegExp("^\d+$");
if (c.match(re) != null)
return false;
// VISA
re = new RegExp("^4[0-9]{12}(?:[0-9]{3})?$");
if (c.match(re) != null)
return "visa";
// VISA ELECTRON
re = new RegExp("^(4){3}");
if (c.match(re) != null)
return "visa";
// MASTERCARD
re = new RegExp("^5[1-5][0-9]{14}$");
if (c.match(re) != null)
return "mastercard";
// DINERS
re = new RegExp("^3(?:0[0-5]|[68][0-9])[0-9]{11}$");
if (c.match(re) != null)
return "diners";
// DISCOVER
re = new RegExp("^6(?:011|5[0-9]{2})[0-9]{12}$");
if (c.match(re) != null)
return "discover";
// ELO
re = new RegExp("^(401178|401179|431274|438935|451416|457393|457631|457632|504175|627780|636297|636369|(506699|5067[0-6]\d|50677[0-8])|(50900\d|5090[1-9]\d|509[1-9]\d{2})|65003[1-3]|(65003[5-9]|65004\d|65005[0-1])|(65040[5-9]|6504[1-3]\d)|(65048[5-9]|65049\d|6505[0-2]\d|65053[0-8])|(65054[1-9]|6505[5-8]\d|65059[0-8])|(65070\d|65071[0-8])|65072[0-7]|(65090[1-9]|65091\d|650920)|(65165[2-9]|6516[6-7]\d)|(65500\d|65501\d)|(65502[1-9]|6550[3-4]\d|65505[0-8]))[0-9]{10,12}");
if (c.match(re) != null)
return "elo";
// AMEX
re = new RegExp("^3[47][0-9]{13}$");
if (c.match(re) != null)
return "amex";
// JCB
re = new RegExp("^35(2[89]|[3-8][0-9])");
if (c.match(re) != null)
return "jcb";
// AURA
re = new RegExp("^50[0-9]{14,17}$");
if (c.match(re) != null)
return "aura";
// HIPERCARD
re = new RegExp("^(38[0-9]{17}|60[0-9]{14}|63[0-9]{14})$");
if (c.match(re) != null)
return "hipercard";
// MAESTRO
re = new RegExp("^(5(018|0[23]|[68])|6(39|7))");
if (c.match(re) != null)
return "maestro";
// CARTOES PARA LIBERAR
re = new RegExp("^(000000000000000[0-9])");
if (c.match(re) != null)
return "visa";
// return "invalid";
return false
}
console.log("---> American Express: " + getCreditCardLabel('376411112222331') );
console.log("---> American Express: " + getCreditCardLabel('378282246310005') );
console.log("---> American Express: " + getCreditCardLabel('371449635398431') );
console.log("---> American Express: " + getCreditCardLabel('341111111111111') );
console.log("---> Aura: " + getCreditCardLabel('5078601870000127985') );
console.log("---> Aura: " + getCreditCardLabel('5078601800003247449') );
console.log("---> Aura: " + getCreditCardLabel('5078601912345600019') );
console.log("---> Diners Club: " + getCreditCardLabel('30569309025904') );
console.log("---> Diners Club: " + getCreditCardLabel('38520000023237') );
console.log("---> Diners Club: " + getCreditCardLabel('30111122223331') );
console.log("---> Discover: " + getCreditCardLabel('6011111111111117') );
console.log("---> Elo: " + getCreditCardLabel('6362970000457013') );
console.log("---> Elo: " + getCreditCardLabel('6362970000457013') );
console.log("---> Elo: " + getCreditCardLabel('6362970000457013') );
console.log("---> Hipercard: " + getCreditCardLabel('6062825624254001') );
console.log("---> Hipercard: " + getCreditCardLabel('6370950000000005') );
console.log("---> Hipercard: " + getCreditCardLabel('3841001111222233334') );
console.log("---> Hipercard: " + getCreditCardLabel('6062825624254001') );
console.log("---> MasterCard: " + getCreditCardLabel('5555666677778884') );
console.log("---> MasterCard: " + getCreditCardLabel('5555555555554444') );
console.log("---> MasterCard: " + getCreditCardLabel('5105105105105100') );
console.log("---> MasterCard: " + getCreditCardLabel('5555666677778884') );
console.log("---> MasterCard: " + getCreditCardLabel('5453010000066167') );
console.log("---> Maestro: " + getCreditCardLabel('6759649826438453') );
console.log("---> Maestro: " + getCreditCardLabel('6799990100000000019') );
console.log("---> Visa: " + getCreditCardLabel('4111111111111111') );
console.log("---> Visa: " + getCreditCardLabel('4012888888881881') );
console.log("---> Visa: " + getCreditCardLabel('4111111111111111') );
console.log("---> Visa: " + getCreditCardLabel('4514160123456789') );
console.log("---> Visa: " + getCreditCardLabel('4073020000000002') );
console.log("---> Visa: " + getCreditCardLabel('4012001037141112') );
console.log("---> Visa (Autorizado): " + getCreditCardLabel('0000000000000001') );
console.log("---> Visa (Não Autorizado): " + getCreditCardLabel('0000000000000002') );
console.log("---> Visa (Cartão Expirado): " + getCreditCardLabel('0000000000000003') );
console.log("---> Visa (Autorizado): " + getCreditCardLabel('0000000000000004') );
console.log("---> Visa (Cartão Bloqueado): " + getCreditCardLabel('0000000000000005') );
console.log("---> Visa (Time Out): " + getCreditCardLabel('0000000000000006') );
console.log("---> Visa (Cartão Cancelado): " + getCreditCardLabel('0000000000000007') );
console.log("---> Visa (Problemas com o Cartão de Crédito): " + getCreditCardLabel('0000000000000008') );
console.log("---> Visa (Autorização Aleatória): " + getCreditCardLabel('0000000000000009') );
// false
console.log("---> COM CARACTER ESPECIAL: " + getCreditCardLabel('637095 0000000£¢¬005') );
console.log("---> COM LETRA: " + getCreditCardLabel('637095a') );
console.log("---> COM ESPAÇO EM BRANCO: " + getCreditCardLabel('00 00000 0000 00001') );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment