Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Validation.add('validate-cpfcnpj', 'CPF/CNPJ inválido.', function(v){
- return validateCPF(v) || validateCNPJ(v);
- });
- function validateCPF(strCPF) {
- var sum;
- var rest;
- sum = 0;
- if (strCPF == "00000000000") return false;
- for (i=1; i<=9; i++) sum = sum + parseInt(strCPF.substring(i-1, i)) * (11 - i);
- rest = (sum * 10) % 11;
- if ((rest == 10) || (rest == 11)) rest = 0;
- if (rest != parseInt(strCPF.substring(9, 10)) ) return false;
- sum = 0;
- for (i = 1; i <= 10; i++) sum = sum + parseInt(strCPF.substring(i-1, i)) * (12 - i);
- rest = (sum * 10) % 11;
- if ((rest == 10) || (rest == 11)) rest = 0;
- if (rest != parseInt(strCPF.substring(10, 11) ) ) return false;
- return true;
- }
- function validateCNPJ(cnpj) {
- cnpj = cnpj.replace(/[^\d]+/g,'');
- if(cnpj == '') return false;
- if (cnpj.length != 14)
- return false;
- // Known invalid CNPJ list
- if (cnpj == "00000000000000" ||
- cnpj == "11111111111111" ||
- cnpj == "22222222222222" ||
- cnpj == "33333333333333" ||
- cnpj == "44444444444444" ||
- cnpj == "55555555555555" ||
- cnpj == "66666666666666" ||
- cnpj == "77777777777777" ||
- cnpj == "88888888888888" ||
- cnpj == "99999999999999")
- return false;
- // Validate Verifier Digit
- size = cnpj.length - 2
- numbers = cnpj.substring(0,size);
- digits = cnpj.substring(size);
- sum = 0;
- pos = size - 7;
- for (i = size; i >= 1; i--) {
- sum += numbers.charAt(size - i) * pos--;
- if (pos < 2)
- pos = 9;
- }
- result = sum % 11 < 2 ? 0 : 11 - sum % 11;
- if (result != digits.charAt(0))
- return false;
- size = size + 1;
- numbers = cnpj.substring(0,size);
- sum = 0;
- pos = size - 7;
- for (i = size; i >= 1; i--) {
- sum += numbers.charAt(size - i) * pos--;
- if (pos < 2)
- pos = 9;
- }
- result = sum % 11 < 2 ? 0 : 11 - sum % 11;
- if (result != digits.charAt(1))
- return false;
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement