-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate-cpf.html
More file actions
128 lines (112 loc) · 3.62 KB
/
validate-cpf.html
File metadata and controls
128 lines (112 loc) · 3.62 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Validate CPF</title>
<style>
body {
height: 100vh;
margin: 0;
font-family: Arial, Helvetica, sans-serif;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
padding: 20px;
}
h1 {
font-size: 24px;
margin-bottom: 5px;
text-align: center;
font-weight: normal;
color: #333333;
margin-top: -5px;
}
p {
font-size: 16px;
margin-bottom: 10px;
line-height: 1.6;
text-align: center;
width: 80%;
margin-left: auto;
margin-right: auto;
margin-top: 0;
}
input {
padding: 10px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 5px;
margin-bottom: 10px;
}
button {
padding: 10px 20px;
cursor: pointer;
font-size: 16px;
border: none;
border-radius: 50px;
background-color: #007bff;
color: white;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #0056b3;
}
#result {
margin-top: 15px;
font-size: 18px;
font-weight: bold;
}
a {
color: #007bff;
text-decoration: none;
margin-top: 15px;
font-size: 14px;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>Validate CPF</h1>
<p>Enter a CPF below to validate it.</p>
<input type="text" id="cpfInput" placeholder="Enter CPF (only numbers)">
<button onclick="validateCPF()">VALIDATE</button>
<div id="result"></div>
<a href="index.html">Back to CPF Generator</a>
<script>
function validateCPF() {
const cpf = document.getElementById("cpfInput").value;
if (!/^\d{11}$/.test(cpf)) {
document.getElementById("result").innerText = "Invalid CPF format. Please enter 11 digits.";
document.getElementById("result").style.color = "red";
return;
}
// Calculate the first digit
let sum1 = 0;
for (let i = 0; i < 9; i++) {
sum1 += parseInt(cpf[i]) * (10 - i);
}
const remainder1 = (sum1 * 10) % 11;
const digit1 = remainder1 === 10 ? 0 : remainder1;
// Calculate the second digit
let sum2 = 0;
for (let i = 0; i < 10; i++) {
sum2 += parseInt(cpf[i]) * (11 - i);
}
const remainder2 = (sum2 * 10) % 11;
const digit2 = remainder2 === 10 ? 0 : remainder2;
// Validate
if (digit1 === parseInt(cpf[9]) && digit2 === parseInt(cpf[10])) {
document.getElementById("result").innerText = "Valid CPF!";
document.getElementById("result").style.color = "green";
} else {
document.getElementById("result").innerText = "Invalid CPF!";
document.getElementById("result").style.color = "red";
}
}
</script>
</body>
</html>