Validação de bi antigo e cartão do cidadão
//INFO - Validação de bi antigo e cartão do cidadão
var CHAR = {
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'A': 10,
'B': 11,
'C': 12,
'D': 13,
'E': 14,
'F': 15,
'G': 16,
'H': 17,
'I': 18,
'J': 19,
'K': 20,
'L': 21,
'M': 22,
'N': 23,
'O': 24,
'P': 25,
'Q': 26,
'R': 27,
'S': 28,
'T': 29,
'U': 30,
'V': 31,
'W': 32,
'X': 33,
'Y': 34,
'Z': 35,
};
{
validateBI: function(value){
if(!value || value.length !== 9 || isNaN(parseInt(value))) {
return false;
}
return this.checkDigit(value.slice(0, 8)) === value[8];
},
checkDigit: function(value) {
var sum = 0;
var len = value.length;
for (var i = 0; i < len; i++) {
sum += value[i] * (len + 1 - i);
}
var mod = sum % 11;
return '' + ((mod === 0 || mod === 1) ? 0 : 11 - mod);
},
validateCC: function(value){
if (typeof value !== 'string' || value.length !== 12) {
return false;
}
return this.calculateSum(value) === 0;
},
calculateSum: function(value) {
var sum = 0;
for (var i = value.length - 1; i >= 0; i--) {
var d = CHAR[value[i]];
if (d === undefined || (i < 9 && d > 9)) {
return false;
}
if (i % 2 === 0) {
d *= 2;
if (d > 9) {
d -= 9;
}
}
sum += d;
}
return sum % 10;
}
}