CATARSE.UserDocument = Backbone.View.extend({ onUserDocumentKeyup: function(e){ var backerId = $('input#backer_id').val(); var projectId = $('input#project_id').val(); var $documentField = $(e.currentTarget); var documentNumber = $documentField.val(); var resultCpf = this.validateCpf(documentNumber); var resultCnpj = this.validateCnpj(documentNumber); if(documentNumber.replace(/[.\-\_ ]/g, '').length > 10) { if(resultCpf || resultCnpj) { $documentField.addClass('ok').removeClass('error'); //$documentField.attr('disabled', true); $.post('/projects/' + projectId + '/backers/' + backerId + '/update_info', { backer: { payer_document: documentNumber } }); } else { $documentField.addClass('error').removeClass('ok'); } } }, validateCpf: function(cpfString){ var product = 0, i, digit; cpfString = cpfString.replace(/[.\-\_ ]/g, ''); var aux = Math.floor(parseFloat(cpfString) / 100); var cpf = aux * 100; var quotient; for(i=0; i<=8; i++){ product += (aux % 10) * (i+2); aux = Math.floor(aux / 10); } digit = product % 11 < 2 ? 0 : 11 - (product % 11); cpf += (digit * 10); product = 0; aux = Math.floor(cpf / 10); for(i=0; i<=9; i++){ product += (aux % 10) * (i+2); aux = Math.floor(aux / 10); } digit = product % 11 < 2 ? 0 : 11 - (product % 11); cpf += digit; return parseFloat(cpfString) === cpf; }, validateCnpj: function(cnpj) { var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais; digitos_iguais = 1; if (cnpj.length < 14 && cnpj.length < 15) return false; for (i = 0; i < cnpj.length - 1; i++) if (cnpj.charAt(i) != cnpj.charAt(i + 1)) { digitos_iguais = 0; break; } if (!digitos_iguais) { tamanho = cnpj.length - 2 numeros = cnpj.substring(0,tamanho); digitos = cnpj.substring(tamanho); soma = 0; pos = tamanho - 7; for (i = tamanho; i >= 1; i--) { soma += numeros.charAt(tamanho - i) * pos--; if (pos < 2) pos = 9; } resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; if (resultado != digitos.charAt(0)) return false; tamanho = tamanho + 1; numeros = cnpj.substring(0,tamanho); soma = 0; pos = tamanho - 7; for (i = tamanho; i >= 1; i--) { soma += numeros.charAt(tamanho - i) * pos--; if (pos < 2) pos = 9; } resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; if (resultado != digitos.charAt(1)) return false; return true; } else return false; } });