forked from gmgomess/jQuery-CPF-CNPJ-Validator-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.cpfcnpj.js
189 lines (153 loc) · 5.41 KB
/
jquery.cpfcnpj.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*!
* jQuery CPF/CNPJ Validator Plugin v1.1.0
* Developed by: Guilherme Gomes ([email protected])
* Date: 2014-10-06
*/
(function ($) {
var type = null;
$.fn.cpfcnpj = function (options) {
// Default settings
var settings = $.extend({
mask: false,
validate: 'cpfcnpj',
event: 'focusout',
handler: $(this),
ifValid: null,
ifInvalid: null
}, options);
if (settings.mask) {
if (jQuery().mask == null) {
settings.mask = false;
console.log("jQuery mask not found.");
}
else {
if (settings.validate == 'cpf') {
$(this).mask('000.000.000-00');
}
else if (settings.validate == 'cnpj') {
$(this).mask('00.000.000/0000-00');
}
else {
var ctrl = $(this);
var opt = {
onKeyPress: function (field) {
var masks = ['000.000.000-009', '00.000.000/0000-00'];
msk = (field.length > 14) ? masks[1] : masks[0];
ctrl.mask(msk, this);
}
};
$(this).mask('000.000.000-009', opt);
}
}
}
return this.each(function () {
var valid = null;
var control = $(this);
$(document).on(settings.event, settings.handler,
function () {
if (control.val().length == 14 || control.val().length == 18) {
if (settings.validate == 'cpf') {
valid = validate_cpf(control.val());
}
else if (settings.validate == 'cnpj') {
valid = validate_cnpj(control.val())
}
else if (settings.validate == 'cpfcnpj') {
if (validate_cpf(control.val())) {
valid = true;
type = 'cpf';
}
else if (validate_cnpj(control.val())) {
valid = true;
type = 'cnpj';
}
else valid = false;
}
}
else valid = false;
if ($.isFunction(settings.ifValid)) {
if (valid != null && valid) {
if ($.isFunction(settings.ifValid)) {
var callbacks = $.Callbacks();
callbacks.add(settings.ifValid);
callbacks.fire(control);
}
}
else if ($.isFunction(settings.ifInvalid)) {
settings.ifInvalid(control);
}
}
});
});
}
function validate_cnpj(val) {
if (val.match(/^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/) != null) {
var val1 = val.substring(0, 2);
var val2 = val.substring(3, 6);
var val3 = val.substring(7, 10);
var val4 = val.substring(11, 15);
var val5 = val.substring(16, 18);
var i;
var number;
var result = true;
number = (val1 + val2 + val3 + val4 + val5);
s = number;
c = s.substr(0, 12);
var dv = s.substr(12, 2);
var d1 = 0;
for (i = 0; i < 12; i++)
d1 += c.charAt(11 - i) * (2 + (i % 8));
if (d1 == 0)
result = false;
d1 = 11 - (d1 % 11);
if (d1 > 9) d1 = 0;
if (dv.charAt(0) != d1)
result = false;
d1 *= 2;
for (i = 0; i < 12; i++) {
d1 += c.charAt(11 - i) * (2 + ((i + 1) % 8));
}
d1 = 11 - (d1 % 11);
if (d1 > 9) d1 = 0;
if (dv.charAt(1) != d1)
result = false;
return result;
}
return false;
}
function validate_cpf(val) {
if (val.match(/^\d{3}\.\d{3}\.\d{3}\-\d{2}$/) != null) {
var val1 = val.substring(0, 3);
var val2 = val.substring(4, 7);
var val3 = val.substring(8, 11);
var val4 = val.substring(12, 14);
var i;
var number;
var result = true;
number = (val1 + val2 + val3 + val4);
s = number;
c = s.substr(0, 9);
var dv = s.substr(9, 2);
var d1 = 0;
for (i = 0; i < 9; i++) {
d1 += c.charAt(i) * (10 - i);
}
if (d1 == 0)
result = false;
d1 = 11 - (d1 % 11);
if (d1 > 9) d1 = 0;
if (dv.charAt(0) != d1)
result = false;
d1 *= 2;
for (i = 0; i < 9; i++) {
d1 += c.charAt(i) * (11 - i);
}
d1 = 11 - (d1 % 11);
if (d1 > 9) d1 = 0;
if (dv.charAt(1) != d1)
result = false;
return result;
}
return false;
}
}(jQuery));