Created
September 3, 2016 06:38
-
-
Save cdvillagra/43822e001d5858c2544ef23e9bbbcf89 to your computer and use it in GitHub Desktop.
Validação de cartão de crédito com referência à tabela IIN e validação com algoritimo de Luhn mod 10
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
//# Cria objeto primario caso não exista | |
if(typeof lavamosnos == 'undefined') lavamosnos = new Object(); | |
//# Cria objeto secundário relacionado à classe quando não existir | |
if(typeof lavamosnos.card == 'undefined') lavamosnos.card = new Object(); | |
var operators = [{ | |
flag : 'visa', | |
maxNumber : [ | |
13,16 | |
], | |
maxCVV : 3, | |
initNumber : [ | |
4 | |
] | |
}, | |
{ | |
flag : 'mastercard', | |
maxNumber : [ | |
16 | |
], | |
maxCVV : 3, | |
initNumber : [ | |
5 | |
] | |
}, | |
{ | |
flag : 'diners', | |
maxNumber : [ | |
14,16 | |
], | |
maxCVV : 3, | |
initNumber : [ | |
301, | |
305, | |
36, | |
38 | |
] | |
}, | |
{ | |
flag : 'elo', | |
maxNumber : [ | |
16 | |
], | |
maxCVV : 3, | |
initNumber : [ 4011, | |
401178, | |
401179, | |
431274, | |
438935, | |
451416, | |
457393, | |
4576, | |
457631, | |
457632, | |
504175, | |
[506699, 506778], | |
[509000, 509999], | |
627780, | |
636297, | |
636368, | |
636369, | |
[650031, 650033], | |
[650035, 650051], | |
[650405, 650439], | |
[650485, 650538], | |
[650541, 650598], | |
[650700, 650718], | |
[650720, 650727], | |
[650901, 650920], | |
[651652, 651679], | |
[655000, 655019], | |
[655021, 655058] | |
] | |
}, | |
{ | |
flag : 'amex', | |
maxNumber : [ | |
15 | |
], | |
maxCVV : 4, | |
initNumber : [ | |
[340000, 349999], | |
[370000, 379999] | |
] | |
}, | |
{ | |
flag : 'discover', | |
maxNumber : [ | |
16 | |
], | |
maxCVV : 4, | |
initNumber : [ | |
6011, | |
622, | |
64, | |
65 | |
] | |
}, | |
{ | |
flag : 'aura', | |
maxNumber : [ | |
16 | |
], | |
maxCVV : 3, | |
initNumber : [ | |
50 | |
] | |
}, | |
{ | |
flag : 'jcb', | |
maxNumber : [ | |
16 | |
], | |
maxCVV : 3, | |
initNumber : [ | |
35 | |
] | |
}, | |
{ | |
flag : 'hipercard', | |
maxNumber : [ | |
13,16,19 | |
], | |
maxCVV : 3, | |
initNumber : [ | |
384100, | |
384140, | |
384160, | |
606282 | |
] | |
}]; | |
//# Decladração das funções dentro do objeto criado | |
lavamosnos.card = { | |
init: function(){ | |
//# check flag | |
$('#numberCard').keyup(lavamosnos.card.checkFlag); | |
//# check numMax Card | |
$('#numberCard').blur(lavamosnos.card.checkNumMax); | |
//# check CVv | |
$('#cvvCard').blur(lavamosnos.card.checkCvv); | |
}, | |
checkFlag: function(){ | |
var number = $('#numberCard').val(); | |
window.flag = false; | |
var base = 0; | |
$.each(operators, function(i,j){ | |
$.each(j.initNumber, function(k,w){ | |
if(typeof(w) == 'number'){ | |
if(number.replace(" ", "").substring(0, w.toString().length) == w.toString()){ | |
if(w.toString().length > base){ | |
base = w.toString().length; | |
flag = j.flag; | |
} | |
} | |
} | |
if(typeof(w) == 'object'){ | |
var numberBase = parseInt(number.replace(" ", "").substring(0, w[0].toString().length)); | |
if( | |
(numberBase >= w[0]) && | |
(numberBase <= w[1]) | |
){ | |
if(w.toString().length > base){ | |
base = w.toString().length; | |
flag = j.flag; | |
} | |
} | |
} | |
var image = false; | |
switch(flag){ | |
case 'visa': | |
image = 'card-visa.png'; | |
break; | |
case 'mastercard': | |
image = 'card-master.png'; | |
break; | |
case 'diners': | |
image = 'card-diners.png'; | |
break; | |
case 'elo': | |
image = 'card-elo.png'; | |
break; | |
case 'amex': | |
image = 'card-amex.png'; | |
break; | |
case 'discover': | |
image = 'card-discover.png'; | |
break; | |
case 'aura': | |
image = 'card-aura.png'; | |
break; | |
case 'jcb': | |
image = 'card-jcb.png'; | |
break; | |
case 'hipercard': | |
image = 'card-hiper.png'; | |
break; | |
} | |
if(image !== false){ | |
$('.card-flag').html('<img src="'+lavamosnos.general.image(image)+'" />'); | |
}else{ | |
$('.card-flag').html(''); | |
} | |
}); | |
}); | |
}, | |
checkNumMax: function(){ | |
var number = $('#numberCard').val(); | |
window.numValidate = false; | |
if(window.flag !== false){ | |
$.each(operators, function(i,j){ | |
if(j.flag == window.flag) | |
if(j.maxNumber.indexOf(number.split(" ").join("").length) != -1) | |
numValidate = true; | |
}); | |
} | |
window.luhn = lavamosnos.card.luhn(number); | |
if((window.flag == 'amex') && (luhn === false)) | |
luhn = lavamosnos.card.luhn(number,true); | |
}, | |
luhn: function(number, reverse){ | |
var odd = []; | |
var pair = []; | |
var tt_odd = 0; | |
var tt_pair = 0; | |
var gsdf = 'erwseafdas'; | |
number = number.split(' ').join(''); | |
var lastKey = parseInt(number.slice(-1)); | |
var chars = number.split(''); | |
if(typeof reverse !== 'undefined') | |
chars = chars.reverse(); | |
$.each(chars,function(j,i){ | |
if((j + 1) < number.length){ | |
if(j % 2 ? 0 : 1){ | |
odd.push(i); | |
}else{ | |
pair.push(i); | |
} | |
}else{ | |
} | |
}); | |
$.each(odd,function(){ | |
var tt = parseInt(this) * 2; | |
if(tt > 9){ | |
var tmp = tt.toString().split(''); | |
tt = parseInt(tmp[0]) + parseInt(tmp[1]); | |
} | |
tt_odd += tt; | |
}); | |
$.each(pair,function(){ | |
tt_pair += parseInt(this); | |
}); | |
var key = ((parseInt((tt_odd + tt_pair) / 10) * 10) + 10) - (tt_odd + tt_pair); | |
return key == lastKey; | |
}, | |
checkCvv: function(){ | |
var cvv = $('#cvvCard').val(); | |
window.cvvValidate = false; | |
if(window.flag !== false){ | |
$.each(operators, function(i,j){ | |
if(j.flag == window.flag) | |
if(j.maxCVV == cvv.toString().length) | |
cvvValidate = true; | |
}); | |
} | |
} | |
} | |
$(document).ready(lavamosnos.card.init); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment