Skip to content

Commit 5232343

Browse files
Add files via upload
1 parent 301f541 commit 5232343

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

validação cpf/assets/js/index.js

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
class ValidacaoCpf {
2+
constructor(cpf) {
3+
this.cpf = cpf;
4+
this.numeroLimpo;
5+
this.numeroLimpoArray = [];
6+
this.verdadeiro;
7+
}
8+
9+
limparCPF() {
10+
this.numeroLimpo = this.cpf;
11+
for (let i = 0; i < this.numeroLimpo.length; i++) {
12+
if (this.numeroLimpo[i] !== '.' && this.numeroLimpo[i] !== '-'){
13+
this.numeroLimpoArray.push(this.numeroLimpo[i])
14+
} else {
15+
continue
16+
}
17+
}
18+
return this.numeroLimpoArray
19+
}
20+
21+
esquecerCPF() {
22+
this.numeroLimpoArray = [];
23+
}
24+
25+
converterCPFEmNumero() {
26+
this.numeroLimpoArray = this.limparCPF().map((value) => parseInt(value))
27+
return this.numeroLimpoArray
28+
}
29+
30+
somaDigitoUm() {
31+
let acum = 10;
32+
const somaPrimeiro = this.converterCPFEmNumero().reduce((acumF, valor, indice, array) => {
33+
if (indice < array.length - 2) {
34+
acumF += valor * acum
35+
acum--
36+
}
37+
return acumF
38+
}, 0)
39+
let primeiroNumero = 11 - (somaPrimeiro % 11);
40+
if (primeiroNumero > 9 ) primeiroNumero = 0;
41+
return primeiroNumero
42+
}
43+
44+
somaDigitoDois() {
45+
let acum = 11;
46+
const somaSegundo = this.numeroLimpoArray.reduce((acumF, valor, indice, array) => {
47+
if (indice < array.length - 1) {
48+
acumF += valor * acum
49+
acum--
50+
}
51+
return acumF
52+
}, 0)
53+
let segundoNumero = 11 - (somaSegundo % 11);
54+
if (segundoNumero > 9 ) segundoNumero = 0;
55+
return segundoNumero
56+
}
57+
58+
verificarSequencia() {
59+
const possivelSequencia = this.numeroLimpoArray.filter((valor) => valor === this.numeroLimpoArray[0])
60+
return !(possivelSequencia.length === 11)
61+
}
62+
63+
verificacaoUm() {
64+
return this.somaDigitoUm() === this.numeroLimpoArray[9];
65+
}
66+
67+
verificacaoDois() {
68+
return this.somaDigitoDois() === this.numeroLimpoArray[10];
69+
}
70+
71+
verificaçãoGeral() {
72+
if(this.verificacaoUm() && this.verificacaoDois() && this.verificarSequencia()) return 'cpf valido';
73+
else return 'cpf invalido'
74+
}
75+
}
76+
77+
78+
79+
80+
const cpf1 = new ValidacaoCpf('031.205.580-36')
81+
console.log(cpf1.limparCPF())
82+
console.log(cpf1.verificaçãoGeral())
83+
84+
document.addEventListener('click', function (event) {
85+
const ev = event.target;
86+
const input = document.querySelector('.verification-input input')
87+
if (ev.classList.contains('verification-button')) {
88+
const cpfDOM = new ValidacaoCpf(input.value);
89+
alert(cpfDOM.verificaçãoGeral())
90+
}
91+
})
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
* {
2+
margin: 0;
3+
padding: 0;
4+
}
5+
:root {
6+
--background-color-one: rgb(2, 120, 112);
7+
--background-color-two: rgb(206, 255, 252);
8+
}
9+
.container {
10+
background-color: var(--background-color-one);
11+
width: 100%;
12+
height: 100vh;
13+
display: flex;
14+
justify-content: center;
15+
align-items: flex-start;
16+
}
17+
.introduction {
18+
width: 60vw;
19+
height: auto;
20+
margin-top: 10vh;
21+
background-color: var(--background-color-two);
22+
padding: 2vmin;
23+
border: solid var(--background-color-two);
24+
border-radius: 10px;
25+
font-size: 1.2em;
26+
display: flex;
27+
gap: 2vmin;
28+
flex-direction: column;
29+
}
30+
.verification-input input{
31+
width: 30vw;
32+
font-size: 1em;
33+
}

validação cpf/index.html

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Document</title>
8+
<link rel="stylesheet" href="./assets/styles/styles.css">
9+
</head>
10+
11+
<body>
12+
<section class="container">
13+
<div class="introduction">
14+
<h1>Verificador de CPF</h1>
15+
<div class="verification">
16+
<div class="verification-input">
17+
<input type="text" placeholder="000.000.000-00" maxlength="14">
18+
<div><button class="verification-button">Verificar</button></div>
19+
</div>
20+
</div>
21+
</div>
22+
</section>
23+
<script src="./assets/js/index.js"></script>
24+
</body>
25+
26+
</html>

0 commit comments

Comments
 (0)