Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
E ai pessoal, tenho um formulario com mascara para data, cpf, pis e telefone. Tambem tem funçao para validar o CPF e o PIS. Aí abaixo estao todas as mascaras e funcoes, tudo esta funcionado no internet explorer, porem no firefox nao funciona nada. Alguem sabe o que deve ser feito para tudo funcionar no firefox tambem?
valeu!
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)
{
pis = remove(pis, ".");
pis = remove(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()
{
var pis = document.cadastro.pis.value;
if (!ChecaPIS(pis))
{
alert("PIS inválido. Tente novamente.");
return false;
} else {
return true;
}
}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;
}
function validar()
{
if(( !validarCPF()) || (!ValidaPis()))
return false;
else
return true;
}
//-->
</script>
<form action="inscricao.php" method="post" name="cadastro" onSubmit="return validar()">
Nome completo: <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>Carregando comentários...