
///*************    Funções de validação de CPF e CNPJ    ******************//

/// Desenvolvida por Alexandre Machado - GTec -  jan/2003
/// Sistema Parceiro Eletronico.

/// Para acessar, chamar a função valida(parâmetro) no trigger: "onBlur" do compo de CPF/CNPJ, 
/// mandando como parâmetros "this.value","obrigatorio" ou "opcional", indicando o tipo de preenchimento.

function validaCpfCnpj(valor,tipo){
	if(valor == "" && tipo=="opcional"){
	}else{
		if (valor == "" && tipo=="obrigatorio"){
			alert("o preenchimento do campo CPF/CNPJ é obrigatório!");
		}else if (valor.length == 11){
			validaCpf(valor);
		}else{
			validaCnpj(valor);
		}
	}
}

<!-- -------------------------  V A L I D A   C P F ----------------------------- -->

function validaCpf(valor) 
{
   if 
   (
	checaCPF(valor))
   {
   	///CPF válido!
	document.assine.nrCpfPag.value = valor;
	document.assine.nrCnpjPag.value = "";
	document.assine.tpPessoaPag.value = "F";
	//alert("CPF válido!");
   }
   else
   {
	errors="1";

   if (errors) 
	alert('CPF/CNPJ incorreto. Digite novamente.');
	document.MM_returnValue = (errors == '');
//	document.form1.campo.focus();
   }
}

function checaCPF (CPF){
	if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" || CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" || CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" || CPF == "88888888888" || CPF == "99999999999")
	return false;

	soma = 0;
	for (i=0; i < 9; i ++)
		soma += parseInt(CPF.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(9)))
		return false;
	soma = 0;
	for (i = 0; i < 10; i ++)
		soma += parseInt(CPF.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(10)))
		return false;
	return true;
}

<!-- -------------------------  V A L I D A   C N P J ----------------------------- -->

function validaCnpj(valor){
	if(VerifyCNPJ(valor) == 1){
		//CNPJ correto!
		document.assine.nrCnpjPag.value = valor;
		document.assine.nrCpfPag.value = "";
		document.assine.tpPessoaPag.value = "J";
		CnpjValido = true;
		//alert("CNPJ correto!");
	}else{
		alert("CPF/CNPJ incorreto. Digite novamente.");
		CnpjValido = false;
	}
//	document.form1.campo.focus();
	return;
}



// Aqui inicia as funções de teste do CNPJ


function isNUMB(c){
	if((cx=c.indexOf(","))!=-1){		
		c = c.substring(0,cx)+"."+c.substring(cx+1);
	}
	if((parseFloat(c) / c != 1)){
		if(parseFloat(c) * c == 0){
			return(1);
		}else{
			return(0);
		}
	}else{
		return(1);
	}
}

function LIMP(c){
	while((cx=c.indexOf("-"))!=-1){		
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf("/"))!=-1){		
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf(","))!=-1){		
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf("."))!=-1){		
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf("("))!=-1){		
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf(")"))!=-1){		
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf(" "))!=-1){		
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	return(c);
}

function VerifyCNPJ(CNPJ){
	CNPJ = LIMP(CNPJ);
	if(isNUMB(CNPJ) != 1){
		return(0);
	}else{
		if(CNPJ == 0){
			return(0);
		}else{
			g=CNPJ.length-2;
			if(RealTestaCNPJ(CNPJ,g) == 1){
				g=CNPJ.length-1;
				if(RealTestaCNPJ(CNPJ,g) == 1){	
					return(1);
				}else{
					return(0);
				}
			}else{
				return(0);
			}
		}
	}
}
	
function RealTestaCNPJ(CNPJ,g){
	var VerCNPJ=0;
	var ind=2;
	var tam;
	for(f=g;f>0;f--){
		VerCNPJ+=parseInt(CNPJ.charAt(f-1))*ind;
		if(ind>8){
			ind=2;
		}else{
			ind++;
		}
	}
	VerCNPJ%=11;
	if(VerCNPJ==0 || VerCNPJ==1){
		VerCNPJ=0;
	}else{
		VerCNPJ=11-VerCNPJ;
	}
	if(VerCNPJ!=parseInt(CNPJ.charAt(g))){
		return(0);
	}else{
		return(1);
	}
}

