Skip to content

Instantly share code, notes, and snippets.

@andrechavesg
Last active April 17, 2024 21:39
Show Gist options
  • Save andrechavesg/ce95c17640624eb4175a8b6ba74e8f5c to your computer and use it in GitHub Desktop.
Save andrechavesg/ce95c17640624eb4175a8b6ba74e8f5c to your computer and use it in GitHub Desktop.
validator.service.ts
import {Injectable} from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ValidatorService {
constructor() {
}
validateCpf(cpf: string): boolean {
let soma;
let resto;
soma = 0;
if (cpf === '00000000000') {
return false;
}
let i = 0;
for (i = 1; i <= 9; i++) {
// tslint:disable-next-line:radix
soma = soma + parseInt(cpf.substring(i - 1, i)) * (11 - i);
}
resto = (soma * 10) % 11;
if ((resto === 10) || (resto === 11)) {
resto = 0;
}
// tslint:disable-next-line:radix
if (resto !== parseInt(cpf.substring(9, 10))) {
return false;
}
soma = 0;
for (i = 1; i <= 10; i++) {
// tslint:disable-next-line:radix
soma = soma + parseInt(cpf.substring(i - 1, i)) * (12 - i);
}
resto = (soma * 10) % 11;
if ((resto === 10) || (resto === 11)) {
resto = 0;
}
// tslint:disable-next-line:radix
return resto === parseInt(cpf.substring(10, 11));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment