Validacao CPF/CNPJ conforme marcação no RadioBox(javascript)

0 respostas
A

Alguém me ajuda, eu tenho um formulario que, se o cara marcar CPF, o campo de texto valida conforme o CPF, se ele marca CNPJ, o campo texte valida conforme CNPJ.

O que que esta acontecendo... Ele so pega o valor do RadioBox como CPF, mesmo que esteja marcado o CNPJ.

Vou colocar os codigos aqui, ve se alguem pode me ajudar:

Formulario:
<tr>
   <td width="20"><label>
      <input type="radio" name="tipoDoc" id="tipoDoc" value="CPF" />
         </label>
   </td>
   <td width="10">&nbsp;</td> 
   <td width="40" class="textoLogin" align="left">CPF</td>
   <td width="10">&nbsp;</td>
   <td width="18" class="textoLogin">ou</td>
   <td width="25">&nbsp;</td>
   <td width="24"><label>
      <input type="radio" name="tipoDoc" id="tipoDoc" value="CNPJ" />
         </label>
   </td>
   <td width="10">&nbsp;</td>
   <td width="83" class="textoLogin" align="left">CNPJ *</td>
</tr>
</table>
</td>
</tr>
<tr>
      <td align="left"><input name="num_doc" type="text" id="num_doc" class="frm_login" onfocus="this.className='frm_login_on';javascript:mascara()" onblur="this.className='frm_login';javascript:verificaCampo(this)" size="25" maxlength="20" /></td>
</tr>
Segue a funcao verificaCampo:
function verificaCampo(num){

		if(!document.getElementById('num_doc').value == ""){
		
			if(document.getElementById('tipoDoc').value == "CPF"){
	
				var numeros, digitos, soma, i, resultado, digitos_iguais, cpfdigitos;
	
				cpfdigitos = num.value.replace(".","");
				cpfdigitos = cpfdigitos.replace(".","");
				cpfdigitos = cpfdigitos.replace("-","");
				
				digitos_iguais = 1;
				if (cpfdigitos.length < 11){
				    alert('CPF inválido');
				    document.getElementById('num_doc').focus();
				    return;
				}
			    
				for (i = 0; i < cpfdigitos.length - 1; i++)
				      if (cpfdigitos.charAt(i) != cpfdigitos.charAt(i + 1))
				            {
				            digitos_iguais = 0;
				            break;
				            }
			    
				if (!digitos_iguais){
				      numeros = cpfdigitos.substring(0,9);
				      digitos = cpfdigitos.substring(9);
				      soma = 0;
				      for (i = 10; i > 1; i--)
				            soma += numeros.charAt(10 - i) * i;
				      resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
				      if (resultado != digitos.charAt(0)){
				    	  alert('CPF inválido');
				    	  document.getElementById('num_doc').focus();
				    	  return;
				      }
				      numeros = cpfdigitos.substring(0,10);
				      soma = 0;
				      for (i = 11; i > 1; i--)
				            soma += numeros.charAt(11 - i) * i;
				      resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
				      if (resultado != digitos.charAt(1))
				    	  return false;
				      return true;
				      }
				else
				      return false;
				
			}
	
			if(document.getElementById('tipoDoc').value == "CNPJ"){
	
				var numeros, digitos, cnpj, soma, i, resultado, pos, tamanho, digitos_iguais;
			      digitos_iguais = 1;
			      cnpj = num;
	
			      alert(cnpj);
			      if (cnpj.length < 14 && cnpj.length < 15){
			    	  alert('CNPJ inválido');
			    	  document.getElementById('num_doc').focus();
			    	  return;
			      }
			      for (i = 0; i < cnpj.length - 1; i++)
			            if (cnpj.charAt(i) != cnpj.charAt(i + 1))
			                  {
			                  digitos_iguais = 0;
			                  break;
			                  }
			      if (!digitos_iguais)
			            {
			            tamanho = cnpj.length - 2
			            numeros = cnpj.substring(0,tamanho);
			            digitos = cnpj.substring(tamanho);
			            soma = 0;
			            pos = tamanho - 7;
			            for (i = tamanho; i >= 1; i--)
			                  {
			                  soma += numeros.charAt(tamanho - i) * pos--;
			                  if (pos < 2)
			                        pos = 9;
			                  }
			            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
			            if (resultado != digitos.charAt(0))
			                  return false;
			            tamanho = tamanho + 1;
			            numeros = cnpj.substring(0,tamanho);
			            soma = 0;
			            pos = tamanho - 7;
			            for (i = tamanho; i >= 1; i--)
			                  {
			                  soma += numeros.charAt(tamanho - i) * pos--;
			                  if (pos < 2)
			                        pos = 9;
			                  }
			            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
			            if (resultado != digitos.charAt(1)){
			            	alert('CPF inválido');
					    	document.getElementById('num_doc').focus();
					    	return;
			            }
			            return true;
			            }
			      else
			            return false;
						
				
			}

		}
			
	}

Quando eu coloco o alert pra testa o valor da radio, mesmo marcando CNPJ, diz que o valor é CPF.

Valeu

Criado 23 de fevereiro de 2010
Respostas 0
Participantes 1