Skip to content

Instantly share code, notes, and snippets.

@guigmaster
Last active January 30, 2020 17:18
Show Gist options
  • Select an option

  • Save guigmaster/1330066d6a3ec70fff13 to your computer and use it in GitHub Desktop.

Select an option

Save guigmaster/1330066d6a3ec70fff13 to your computer and use it in GitHub Desktop.
Custom's Parsley Validator's
'use strict';
(function($) {
window.ParsleyValidator
.addValidator('validcnpj', function (value, requirement) {
var cnpj = value.replace(/[^0-9]/g, '')
, len = cnpj.length - 2
, numbers = cnpj.substring(0,len)
, digits = cnpj.substring(len)
, add = 0
, pos = len - 7
, invalidCNPJ = [
'00000000000000',
'11111111111111',
'22222222222222',
'33333333333333',
'44444444444444',
'55555555555555',
'66666666666666',
'77777777777777',
'88888888888888',
'99999999999999'
]
, result
;
if ( cnpj.length < 11 || $.inArray(cnpj, invalidCNPJ) !== -1 ) {
return false;
}
for (i = len; i >= 1; i--) {
add = add + parseInt(numbers.charAt(len - i)) * pos--;
if (pos < 2) { pos = 9; }
}
result = (add % 11) < 2 ? 0 : 11 - (add % 11);
if (result != digits.charAt(0)) {
return false;
}
len = len + 1;
numbers = cnpj.substring(0,len);
add = 0;
pos = len - 7;
for (i = 13; i >= 1; i--) {
add = add + parseInt(numbers.charAt(len - i)) * pos--;
if (pos < 2) { pos = 9; }
}
result = (add % 11) < 2 ? 0 : 11 - (add % 11);
if (result != digits.charAt(1)) {
return false;
}
return true;
}, 32)
.addMessage('pt-br', 'validcnpj', 'Este campo deve ser um CNPJ válido.');
}(window.jQuery));
'use strict';
(function($) {
window.ParsleyValidator
.addValidator('validcpf', function (value, requirement) {
var cpf = value.replace(/[^0-9]/g, '')
, compareCPF = cpf.substring(0, 9)
, add = 0
, i, u
, invalidCPF = [
'00000000000',
'11111111111',
'22222222222',
'33333333333',
'44444444444',
'55555555555',
'66666666666',
'77777777777',
'88888888888',
'99999999999'
]
;
if ( cpf.length < 11 || $.inArray(cpf, invalidCPF) !== -1 ) {
return false;
}
for (i = 8, u = 2; i >= 0; i--, u++) {
add = add + parseInt(cpf.substring(i, i+1)) * u;
}
compareCPF = compareCPF + ( (add % 11) < 2 ? 0 : 11 - (add % 11));
add = 0
for (i = 9, u = 2; i >= 0; i--, u++) {
add = add + parseInt(cpf.substring(i, i+1)) * u;
}
compareCPF = compareCPF + ( (add % 11) < 2 ? 0 : 11 - (add % 11));
if (compareCPF !== cpf) {
return false;
}
return true;
}, 32)
.addMessage('pt-br', 'validcpf', 'Este campo deve ser um CPF válido.');
}(window.jQuery));
@cyberrep
Copy link
Copy Markdown

Needs to add "i" variable to work on validcnpj.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment