Dim emissao(10) As String
If Not isNumeric(CPF, text) Then
ElseIf Not isValid(CPF, text) Then
CPF = CPF.Replace(".", "").Replace("-", "")
For i = 0 To emissao.Length - 1
Select Case CPF.Substring(8, 1)
If emissao(i).Contains(",") Then
Console.WriteLine("CPF emitido em um dos seguintes estados brasileiros: " & emissao(i))
Console.WriteLine("CPF emitido em " & emissao(i))
Private Function isValid(Byval value As String, ByRef text As String) As Boolean
Dim count As Integer = 10
If Not value.Length = 11 And Not value.Length = 14 Then
text = "Dado informado não é um CPF."
ElseIf isFormatted(value) = False Then
text = "Formato de CPF inválido."
cpf = value.Replace(".", "").Replace("-", "")
cpf = cpf.Substring(0, 9)
For i = 0 To cpf.Length - 1
chr(i) = cpf.Substring(i, 1)
If rest = 0 Or rest = 1 Then
For i = 0 To cpf.Length - 1
chr(i) = cpf.Substring(i, 1)
if rest = 0 Or rest = 1 Then
should = cpf.Insert(3, ".").insert(7, ".").Insert(11, "-")
If Not cpf = value.Replace(".", "").Replace("-", "") Then
if Not value.Contains(".") or Not value.Contains("-") Then
cpf = value.Insert(3, ".").insert(7, ".").Insert(11, "-")
text = "CPF " & cpf & " inválido. Tente usar " & should & "."
text = "CPF " & cpf & " é matematicamente válido."
text = "Houve um erro ao verificar o CPF." & ex.Message.ToString
Private Function isFormatted(Byval value As String) As Boolean
Dim index() As Integer = {3, 7, 11}
Dim chr() As String = {".", ".", "-"}
If Not value.Contains(".") Or Not value.Contains("-") Then
For x = 0 To index.Length - 1
If Not value.Substring(index(x), 1).Contains(chr(x)) Then
Private Function isNumeric(Byval value As String, ByRef text As String) As Boolean
Dim chr() As String = {"a", "b", "c", "d", "e", ", f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
For x = 0 To chr.Length - 1
If value.Contains(chr(x)) Or value.Contains(chr(x).ToUpper) Then
text = "CPF não deve conter letras."
Private Sub local(ByRef emissao() As String)
emissao(0) = "Rio Grande do Sul."
emissao(1) = "Distrito Federal, Goiás, Mato Grosso do Sul ou Tocantins"
emissao(2) = "Pará, Amazonas, Acre, Amapá, Rondônia ou Roraima"
emissao(3) = "Ceará, Maranhão ou Piauí"
emissao(4) = "Pernambuco, Rio Grande do Norte, Paraíba ou Alagoas"
emissao(5) = "Bahia ou Sergipe"
emissao(6) = "Minas Gerais"
emissao(7) = "Rio de Janeiro ou Espírito Santo"
emissao(9) = "Paraná ou Santa Catarina"