Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Besouro

[Resolvido] Validar PIS

Recommended Posts

E ai pessoal, to com um probleminha aqui...

Fiz um formulario pra ser preenchido o CPF, data de nascimento, PIS, etc... se alguem tiver precisando das mascaras pega ai q ta tudo funcionando, a validação de CPF tb ta...

 

mas o PIS tb precisa ser validado, tentei colocar a funcao no form de tudo q eh jeito e nao funcionou. Alguem sabe como coloco no form essa funcao?

 

valeuu

 

Besouro

<script type="text/Javascript">
<!--
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' incorretamente.\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' \n'; }
} if (errors) alert('Preencha corretamente:\n'+errors);
document.MM_returnValue = (errors == '');
}

function Mascara (formato, keypress, objeto){
campo = eval (objeto);


// MASCARA DO CPF

if (formato=='cpf'){
separador1 = '.'; 
separador2 = '-'; 
conjunto1 = 3;
conjunto2 = 7;
conjunto3 = 11;
if (campo.value.length == conjunto1)
{
campo.value = campo.value + separador1;
}
if (campo.value.length == conjunto2)
{
campo.value = campo.value + separador1;
}
if (campo.value.length == conjunto3)
{
campo.value = campo.value + separador2;
}
}


// MASCARA DO PIS
if (formato=='pis'){
separador1 = '.'; 
separador2 = '-'; 
conjunto1 = 3;
conjunto2 = 9;
conjunto3 = 12;
if (campo.value.length == conjunto1)
{
campo.value = campo.value + separador1;
}
if (campo.value.length == conjunto2)
{
campo.value = campo.value + separador1;
}
if (campo.value.length == conjunto3)
{
campo.value = campo.value + separador2;
}
}



// MASCARA DO NASCIMENTO
if (formato=='nascimento'){
separador = '/'; 
conjunto1 = 2;
conjunto2 = 5;
if (campo.value.length == conjunto1)
{
campo.value = campo.value + separador;
}
if (campo.value.length == conjunto2)
{
campo.value = campo.value + separador;
}
}

// MASCARA DO TELEFONE
if (formato=='telefone'){
separador1 = '(';
separador2 = ')';
separador3 = '-';
conjunto1 = 0;
conjunto2 = 3;
conjunto3 = 8;
if (campo.value.length == conjunto1){
campo.value = campo.value + separador1;
}
if (campo.value.length == conjunto2){
campo.value = campo.value + separador2;
}
if (campo.value.length == conjunto3){
campo.value = campo.value + separador3;
}
}


}


// VERIFICAR O PIS

var ftap="3298765432";
var total=0;
var i;
var resto=0;
var numPIS=0;
var strResto="";

function ChecaPIS(pis)
{

total=0;
resto=0;
numPIS=0;
strResto="";

numPIS=pis;

if (numPIS=="" || numPIS==null)
{
return false;
}

for(i=0;i<=9;i++)
{
resultado = (numPIS.slice(i,i+1))*(ftap.slice(i,i+1));
total=total+resultado;
}

resto = (total % 11)

if (resto != 0)
{
resto=11-resto;
}

if (resto==10 || resto==11)
{
strResto=resto+"";
resto = strResto.slice(1,2);
}

if (resto!=(numPIS.slice(10,11)))
{
return false;
}

return true;
}

// VALIDAR O PIS

function ValidaPis()
{
pis=document.cadastro.numPIS.value;

if (!ChecaPIS(pis))
{
alert("PIS INVALIDO");
} else {
alert("PIS VALIDO");
}
}

// VALIDAR O CPF

function validarCPF(){
var cpf = document.cadastro.cpf.value;
var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
if(!filtro.test(cpf)){
window.alert("CPF inválido. Tente novamente.");
return false;
}

cpf = remove(cpf, ".");
cpf = remove(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"){
window.alert("CPF inválido. Tente novamente.");
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))){
window.alert("CPF inválido. Tente novamente.");
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))){
window.alert("CPF inválido. Tente novamente.");
return false;
}
return true;
}

function remove(str, sub) {
i = str.indexOf(sub);
r = "";
if (i == -1) return str;
r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
return r;
}

//-->
</script>


<style type="text/css">
<!--
.style2 {color: #FFFFFF}
.style11 {font-size: 8px; color: #000000; font-weight: bold; }
.style12 {color: #FF0000}
-->
</style>




<form action="inscricao.php" method="post" name="cadastro" onsubmit="return validarCPF()">
<input type="text" size="50" name="nome" />
Data de nascimento:<INPUT class=campoform onkeypress="Mascara('nascimento', window.event.keyCode, 'document.cadastro.nascimento');"maxLength=10 size=10 name=nascimento>
RG:<input type="text" name="rg" size="30" />
CPF: <INPUT class=campoform onkeypress="Mascara('cpf', window.event.keyCode, 'document.cadastro.cpf');" maxLength=14 size=14 name="cpf" > 

PIS: <INPUT class=campoform onkeypress="Mascara('pis', window.event.keyCode, 'document.cadastro.pis');" maxLength=14 size=14 name="pis"> 
E-mail:<input type="text" name="email" size="50" />
Telefone:<INPUT class=campoform onkeypress="Mascara('telefone', window.event.keyCode, 'document.cadastro.telefone');" maxLength=13 size=12 name=telefone> 
<input type="checkbox" name="funcionario" value="sim"> Sou funcionário 

<input name="enviar" type="submit" onclick="MM_validateForm('nome','','R','nascimento','','R','rg','','R','cpf','','R','pis','','R','email','','RisEmail','telefone','','R');return document.MM_returnValue" value="Cadastrar-me ">

<input type="reset" name="limpar" value="Limpar o Formulário">
</form>

Compartilhar este post


Link para o post
Compartilhar em outros sites

como fica no form?!

 

onsubmit="return validarCPF()">

 

eu coloco no onsubmit a funçao de validar o pis?

 

ja tentei isso e nao funcionou :(

Compartilhar este post


Link para o post
Compartilhar em outros sites

Adicione a seguinte função:

function validar()
{
	if( !validarCPF() || !ValidaPis() )
		return false;
	else
		return true;
}
e ai chame ela no onsubmit:

<form action="inscricao.php" method="post" name="cadastro" onsubmit="return validar()">
para isso, é preciso alterar a função ValidaPis tb, ficando assim:

function ValidaPis()
{
	var pis = document.cadastro.pis.value;

	if (!ChecaPIS(pis))
	{
		alert("PIS INVALIDO");
		return false;
	} else {
		alert("PIS VALIDO");
		return false;
	}
}
se você seguir corretamente esses passos, irá funcionar.

Compartilhar este post


Link para o post
Compartilhar em outros sites

funcionou mesmo!

 

bastou soh acrescentar na funçao ChecaPIS(pis)

 

pis = remove(pis, ".");

pis = remove(pis, "-");

 

pois ta com a mascara, colocando os pontos e o hifen

 

muito obrigado!!

 

Besouro

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.