Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!--
- Elton Pereira - eltondeveloper@gmail.com
- Função de validação CNPJ
- -->
- function ValidaCNPJ(cnpj) {
- cnpj = cnpj.replace(/[^\d]+/g,'');
- if(cnpj == '') return false;
- if (cnpj.length != 14)
- return false;
- if (cnpj == "00000000000000" ||
- cnpj == "11111111111111" ||
- cnpj == "22222222222222" ||
- cnpj == "33333333333333" ||
- cnpj == "44444444444444" ||
- cnpj == "55555555555555" ||
- cnpj == "66666666666666" ||
- cnpj == "77777777777777" ||
- cnpj == "88888888888888" ||
- cnpj == "99999999999999")
- return false;
- 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;
- }
- <!-- Para utilizar a função aplique -->
- window.onload = function (){
- var cnpj = document.getElementById('reg_billing_cnpj');
- cnpj.onblur = function(){
- var valid = ValidaCNPJ(this.value);
- if(valid == false){
- alert("Informe um CNPJ válido");
- cnpj.focus();
- return (false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement