Skip to content

Commit ddb6de6

Browse files
committed
Added function for CPF validation
The Cadastro de Pessoas Físicas (CPF) – Portuguese for Natural Persons Register – is a number attributed by the Brazilian revenue agency (Receita Federal – Federal Revenue) to both Brazilians and resident aliens who pay taxes or take part, directly or indirectly, in activities that provide revenue for any of the dozens of different types of taxes existing in Brazil. By means of this universal number the Federal Revenue computers can estimate the income tax that is due, thus directing fiscalisation.
1 parent 95f7204 commit ddb6de6

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

js/languages/jquery.validationEngine-pt_BR.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,30 @@
116116
// Brazilian (Real - R$) money format
117117
"regex": /^([1-9]{1}[\d]{0,2}(\.[\d]{3})*(\,[\d]{0,2})?|[1-9]{1}[\d]{0,}(\,[\d]{0,2})?|0(\,[\d]{0,2})?|(\,[\d]{1,2})?)$/,
118118
"alertText": "* Número decimal inválido"
119+
},
120+
"cpf": {
121+
// CPF is the Brazilian ID
122+
"func": function(field, rules, i, options){
123+
cpf = field.val().replace(/[^0-9]+/g, '');
124+
while(cpf.length < 11) cpf = "0"+ cpf;
125+
126+
var expReg = /^0+$|^1+$|^2+$|^3+$|^4+$|^5+$|^6+$|^7+$|^8+$|^9+$/;
127+
var a = cpf.split('');
128+
var b = new Number;
129+
var c = 11;
130+
b += (a[9] * --c);
131+
if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
132+
b = 0;
133+
c = 11;
134+
for (y=0; y<10; y++) b += (a[y] * c--);
135+
if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
136+
137+
var error = false;
138+
if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10]) || cpf.match(expReg)) error = true;
139+
return !error;
140+
},
141+
"alertText": "CPF inválido",
142+
"alertTextOK": "CPF válido"
119143
}
120144
};
121145

0 commit comments

Comments
 (0)