-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidatorCPF.js
51 lines (40 loc) · 963 Bytes
/
ValidatorCPF.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
import Validator from './Validator';
export default class ValidatorCPF extends Validator {
constructor(defaultMessage) {
super(defaultMessage);
}
valid(message) {
this.addStep(
'cpf',
function(v) {
return validate(v);
},
message
);
}
}
function validate(rawCpf) {
var cpf = rawCpf.replace(/\.|-|\s/g, '')
if ( cpf.length != 11 ) return false;
if (/^(.)\1+$/.test(cpf)) return false;
cpf = cpf.split('');
var sum1 = 0;
for( let c = 10; c>=2 ; c--){
sum1 += c * cpf[10-c];
}
var checkSum1 = sum1 % 11
checkSum1 = checkSum1 < 2
? 0
: 11 - checkSum1;
if (cpf[9] != checkSum1) return false;
var sum2 = 0;
for( let c = 11; c>=2 ; c--){
sum2 += c*cpf[11-c];
}
var checkSum2 = sum2 % 11
checkSum2 = checkSum2 < 2
? 0
: 11 - checkSum2;
if (cpf[10] != checkSum2) return false;
return true;
}