Guest User

Untitled

a guest
Feb 18th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. jQuery(function(){
  2. Validation.add('validate-cpf-cnpj', 'Enter a valid CPF/CNPJ.', function validaCPFCNPJ(str) {
  3. str = str.replace(/[^\d]+/g,'');
  4. if (str.length > 11) {
  5. return validaCNPJ(str);
  6. }
  7. return validaCPF(str);
  8.  
  9. });
  10.  
  11. function validaCPF(cpf) {
  12. var numeros,
  13. digitos,
  14. soma,
  15. i,
  16. resultado,
  17. digitos_iguais;
  18. digitos_iguais = 1;
  19.  
  20. if (cpf.length < 11)
  21. return false;
  22.  
  23. for (i = 0; i < cpf.length - 1; i++)
  24. if (cpf.charAt(i) != cpf.charAt(i + 1)) {
  25. digitos_iguais = 0;
  26. break;
  27. }
  28.  
  29. if (!digitos_iguais) {
  30. numeros = cpf.substring(0, 9);
  31. digitos = cpf.substring(9);
  32. soma = 0;
  33.  
  34. for (i = 10; i > 1; i--)
  35. soma += numeros.charAt(10 - i) * i;
  36. resultado = soma % 11 < 2
  37. ? 0
  38. : 11 - soma % 11;
  39.  
  40. if (resultado != digitos.charAt(0))
  41. return false;
  42. numeros = cpf.substring(0, 10);
  43. soma = 0;
  44.  
  45. for (i = 11; i > 1; i--)
  46. soma += numeros.charAt(11 - i) * i;
  47. resultado = soma % 11 < 2
  48. ? 0
  49. : 11 - soma % 11;
  50.  
  51. if (resultado != digitos.charAt(1))
  52. return false;
  53. return true;
  54. } else
  55. return false;
  56. }
  57.  
  58. function validaCNPJ(cnpj) {
  59. if(cnpj == '') return false;
  60.  
  61. if (cnpj.length != 14)
  62. return false;
  63.  
  64. // Elimina CNPJs invalidos conhecidos
  65. if (cnpj == "00000000000000" ||
  66. cnpj == "11111111111111" ||
  67. cnpj == "22222222222222" ||
  68. cnpj == "33333333333333" ||
  69. cnpj == "44444444444444" ||
  70. cnpj == "55555555555555" ||
  71. cnpj == "66666666666666" ||
  72. cnpj == "77777777777777" ||
  73. cnpj == "88888888888888" ||
  74. cnpj == "99999999999999")
  75. return false;
  76.  
  77. // Valida DVs
  78. tamanho = cnpj.length - 2
  79. numeros = cnpj.substring(0,tamanho);
  80. digitos = cnpj.substring(tamanho);
  81. soma = 0;
  82. pos = tamanho - 7;
  83. for (i = tamanho; i >= 1; i--) {
  84. soma += numeros.charAt(tamanho - i) * pos--;
  85. if (pos < 2)
  86. pos = 9;
  87. }
  88. resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
  89. if (resultado != digitos.charAt(0))
  90. return false;
  91.  
  92. tamanho = tamanho + 1;
  93. numeros = cnpj.substring(0,tamanho);
  94. soma = 0;
  95. pos = tamanho - 7;
  96. for (i = tamanho; i >= 1; i--) {
  97. soma += numeros.charAt(tamanho - i) * pos--;
  98. if (pos < 2)
  99. pos = 9;
  100. }
  101. resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
  102. if (resultado != digitos.charAt(1))
  103. return false;
  104.  
  105. return true;
  106. };
  107.  
  108. //MASCARA CPF E CNPJ NO MESMO CAMPO
  109. var options = {onKeyPress: function(cpf, e, f, options){
  110. var masks = ['000.000.000-009', '00.000.000/0000-00'];
  111. mask = (cpf.length>14) ? masks[1] : masks[0];
  112. jQuery('.validate-cpf-cnpj').mask(mask, options);
  113. }};
  114.  
  115. jQuery('.validate-cpf-cnpj').mask('000.000.000-009', options);
  116.  
  117.  
  118. });
Add Comment
Please, Sign In to add comment