Inquiridor
Como usar o mesmo text box pra validação de cpf e cnpj?

Pergunta
-
Olá, então só pra esclarecer mais:
Quando a pessoa selecioar no radio box:pessoa fisica, digitar o cpf e clicar no botão validar ele valide o cpf, e o mesmo com o cnpj, mas tudo no mesmo botão e no mesmo text box.
e uma pergunta tmbm como faço pra que a validação do cnpj não aceite o valor 000000000000.
Obrigado.
Todas as Respostas
-
Olá,
Dê uma olhada neste post: http://social.msdn.microsoft.com/forums/pt-BR/vscsharppt/thread/c85d5abc-de8f-4d87-8793-6ddd9c673677
Fabio Rosa.
-
-
Public Function ValidaCNPJ(ByVal CNPJ As String) As Boolean
Dim i As Integer
Dim valida As Boolean
CNPJ = CNPJ.Trim
For i = 0 To dadosArray.Length - 1
If CNPJ.Length <> 18 Or dadosArray(i).Equals(CNPJ) Then
Return False
End If
Next
'remove a maskara
CNPJ = CNPJ.Substring(0, 2) + CNPJ.Substring(3, 3) + CNPJ.Substring(7, 3) + CNPJ.Substring(11, 4) + CNPJ.Substring(16)
valida = efetivaValidacao(CNPJ)
If valida Then
ValidaCNPJ = True
Else
ValidaCNPJ = False
End If
End Function- Sugerido como Resposta Ninho_Blink quarta-feira, 29 de maio de 2013 17:46
-
Ahh, desculpe...
Para Vb. NET veja este link: http://www.macoratti.net/vbn_vdoc.htm
ou este:
http://www.vbweb.com.br/dicas_visual.asp?Codigo=2403&Classificacao=14
Fabio Rosa.
- Sugerido como Resposta Ninho_Blink quarta-feira, 29 de maio de 2013 17:46
-
Bom dia amigo!
Conseguiu resolver seu problema? Precisa de ajuda ainda? Eu faço validação de cnpj e cpf no mesmo textbox. A única diferença é que eu uso um combobox para escolher pessoa física ou jurídica.
Se ainda estiver precisando de ajuda, de um alô.
Att.
Giovani
- Sugerido como Resposta Programar é viver quarta-feira, 29 de maio de 2013 17:12
-
Olá Giovani, vi que você oferceu ajuda ao amigo Sr. Basic, estou precisando fazer exatamento como você descreveu que faz, consegui fazer com o CPF, mais adiciono o CNPJ, da erro, não consegui resolver, teria como dar uma força?
Eis meu código para o CPF:
Private Sub Btnpesquisar_Click(sender As Object, e As EventArgs) Handles Btnpesquisar.Click
Dim blnResultado As Boolean
Dim strCPF As String
Dim Numero(13) As Integer
Dim soma As Integer'Limpa a mascara.
strCPF = maskcpf.Text
strCPF = Replace(strCPF, ".", "")
strCPF = Replace(strCPF, "/", "")
strCPF = Replace(strCPF, "-", "")
'Verifica se numerico e valores invalidos
If Not IsNumeric(strCPF) Or Len(strCPF) <> 11 Or strCPF = "00000000000" Or strCPF = "11111111111" Or strCPF = "22222222222" Or strCPF = "333333333333" Or
End Sub
strCPF = "44444444444" Or strCPF = "55555555555" Or strCPF = "66666666666" Or strCPF = "77777777777" Or strCPF = "88888888888" Or strCPF = "99999999999" Then
blnResultado = False
Else
Dim Conta As Integer, Resto As Integer, Passo As Integer
strCPF = Trim(strCPF)
For Passo = 11 To 12
soma = 0
For Conta = 1 To Passo - 2
soma = soma + Val(Mid(strCPF, Conta, 1)) * (Passo - Conta)
Next
Resto = 11 - (soma - (Int(soma / 11) * 11))
If Resto = 10 Or Resto = 11 Then Resto = 0
If Resto <> Val(Mid(strCPF, Passo - 1, 1)) Then
blnResultado = False
Else
blnResultado = True
End If
Next
End If
If blnResultado = True Then
MessageBox.Show("Válido")
Else
MessageBox.Show("Dados Invalidos")
End If
End ClassComo faço para adicionar o CNPJ ?
- Editado Gilmar Souza quinta-feira, 30 de outubro de 2014 00:49
-
Bom dia amigo!
Conseguiu resolver seu problema? Precisa de ajuda ainda? Eu faço validação de cnpj e cpf no mesmo textbox. A única diferença é que eu uso um combobox para escolher pessoa física ou jurídica.
Se ainda estiver precisando de ajuda, de um alô.
Att.
Giovani
Olá Giovani, vi que você oferceu ajuda ao amigo Sr. Basic, estou precisando fazer exatamento como você descreveu que faz, consegui fazer com o CPF, mais adiciono o CNPJ, da erro, não consegui resolver, teria como dar uma força?
Eis meu código para o CPF:
Private Sub Btnpesquisar_Click(sender As Object, e As EventArgs) Handles Btnpesquisar.Click
Dim blnResultado As Boolean
Dim strCPF As String
Dim Numero(13) As Integer
Dim soma As Integer'Limpa a mascara.
strCPF = maskcpf.Text
strCPF = Replace(strCPF, ".", "")
strCPF = Replace(strCPF, "/", "")
strCPF = Replace(strCPF, "-", "")'Verifica se numerico e valores invalidos
If Not IsNumeric(strCPF) Or Len(strCPF) <> 11 Or strCPF = "00000000000" Or strCPF = "11111111111" Or strCPF = "22222222222" Or strCPF = "333333333333" Or
End Sub
strCPF = "44444444444" Or strCPF = "55555555555" Or strCPF = "66666666666" Or strCPF = "77777777777" Or strCPF = "88888888888" Or strCPF = "99999999999" Then
blnResultado = False
Else
Dim Conta As Integer, Resto As Integer, Passo As Integer
strCPF = Trim(strCPF)
For Passo = 11 To 12
soma = 0
For Conta = 1 To Passo - 2
soma = soma + Val(Mid(strCPF, Conta, 1)) * (Passo - Conta)
Next
Resto = 11 - (soma - (Int(soma / 11) * 11))
If Resto = 10 Or Resto = 11 Then Resto = 0
If Resto <> Val(Mid(strCPF, Passo - 1, 1)) Then
blnResultado = False
Else
blnResultado = True
End If
Next
End If
If blnResultado = True Then
MessageBox.Show("Válido")
Else
MessageBox.Show("Dados Invalidos")
End If
End ClassComo faço para adicionar o CNPJ ?
-
Sugestão para as 2 funções:
Private Function ValidaCPF(ByVal CPF As String) As Boolean Dim i, x, n1, n2 As Integer CPF = CPF.Trim For i = 0 To dadosArray.Length - 1 If CPF.Length <> 14 Or dadosArray(i).Equals(CPF) Then Return False End If Next 'remove a maskara CPF = CPF.Substring(0, 3) + CPF.Substring(4, 3) + CPF.Substring(8, 3) + CPF.Substring(12) For x = 0 To 1 n1 = 0 For i = 0 To 8 + x n1 = n1 + Val(CPF.Substring(i, 1)) * (10 + x - i) Next n2 = 11 - (n1 - (Int(n1 / 11) * 11)) If n2 = 10 Or n2 = 11 Then n2 = 0 If n2 <> Val(CPF.Substring(9 + x, 1)) Then Return False End If Next Return True End Function
Private Function ValidaCNPJ(ByVal CNPJ As String) As Boolean Dim i As Integer Dim valida As Boolean CNPJ = CNPJ.Trim For i = 0 To dadosArray.Length - 1 If CNPJ.Length <> 18 Or dadosArray(i).Equals(CNPJ) Then Return False End If Next 'remove a maskara CNPJ = CNPJ.Substring(0, 2) + CNPJ.Substring(3, 3) + CNPJ.Substring(7, 3) + CNPJ.Substring(11, 4) + CNPJ.Substring(16) valida = efetivaValidacao(CNPJ) If valida Then ValidaCNPJ = True Else ValidaCNPJ = False End If End Function Private Function efetivaValidacao(ByVal cnpj As String) Dim Numero(13) As Integer Dim soma As Integer Dim i As Integer Dim valida As Boolean Dim resultado1 As Integer Dim resultado2 As Integer For i = 0 To Numero.Length - 1 Numero(i) = CInt(cnpj.Substring(i, 1)) Next soma = Numero(0) * 5 + Numero(1) * 4 + Numero(2) * 3 + Numero(3) * 2 + Numero(4) * 9 + Numero(5) * 8 + Numero(6) * 7 + _ Numero(7) * 6 + Numero(8) * 5 + Numero(9) * 4 + Numero(10) * 3 + Numero(11) * 2 soma = soma - (11 * (Int(soma / 11))) If soma = 0 Or soma = 1 Then resultado1 = 0 Else resultado1 = 11 - soma End If If resultado1 = Numero(12) Then soma = Numero(0) * 6 + Numero(1) * 5 + Numero(2) * 4 + Numero(3) * 3 + Numero(4) * 2 + Numero(5) * 9 + Numero(6) * 8 + _ Numero(7) * 7 + Numero(8) * 6 + Numero(9) * 5 + Numero(10) * 4 + Numero(11) * 3 + Numero(12) * 2 soma = soma - (11 * (Int(soma / 11))) If soma = 0 Or soma = 1 Then resultado2 = 0 Else resultado2 = 11 - soma End If If resultado2 = Numero(13) Then Return True Else Return False End If Else Return False End If End Function
Se adicionar as 2 funções na sua classe, basta chamar depois:
'Para validar CPFs If ValidaCPF(maskcpf.Text) Then MsgBox("CPF válido") Else MsgBox("CPF Inválido") End If 'Para validar CNPJs If ValidaCNPJ(maskcnpj.Text) Then MsgBox("CNPJ válido") Else MsgBox("CNPJ Inválido") End If
Neste link tem o tutorial completo.
Marque como resposta se foi útil.
- Editado Deric Ferreira quinta-feira, 30 de outubro de 2014 02:26
- Sugerido como Resposta Giovani Rodrigues quinta-feira, 30 de outubro de 2014 09:10