-
Notifications
You must be signed in to change notification settings - Fork 2
/
br-cpf-validator.html
32 lines (25 loc) · 1.08 KB
/
br-cpf-validator.html
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-validator-behavior/iron-validator-behavior.html">
<script>
Polymer({
is: 'br-cpf-validator',
behaviors: [
Polymer.IronValidatorBehavior
],
reduceSum: function (previousValue, currentValue) {
return previousValue + currentValue;
},
validate: function (cpf) {
cpf = cpf.toString().replace(/[^0-9]+/g, '');
var soma = [];
var soma2 = [];
for (var i = 0; i < 10; i++) {
if (cpf == i.toString().repeat(11) || cpf == '12345678909') return false;
if (i < 9) soma.push(parseInt(cpf[i]) * (10 - i));
soma2.push(parseInt(cpf[i]) * (11 - i));
}
if (((soma.reduce(this.reduceSum) % 11) < 2 ? 0 : 11 - (soma.reduce(this.reduceSum) % 11)) != cpf[9]) return false;
return (((soma2.reduce(this.reduceSum) % 11) < 2 ? 0 : 11 - (soma2.reduce(this.reduceSum) % 11)) != cpf[10]) ? false : true;
}
});
</script>