Ir para conteúdo

POWERED BY:

Arquivado

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

guma

Incluir funções em java script

Recommended Posts

Gostaria de saber como eu incluo uma função de java script para um codigo html..Por exemplo tenho a validação do CPF.. Dai toda a vez que eu quizer validar o CPF é so chamar minha Função..Alguém pode me dar uma dica ou uma Mão ai ????????Flw

Compartilhar este post


Link para o post
Compartilhar em outros sites

qualquer coisa retorne.

 

function Verifica_CPF(formulario) {var CPF = formulario.CPF.value; if (CPF == '') {  alert('Este campo é de preenchimento obrigatório!');  return false;   }var POSICAO, I, SOMA, DV, DV_INFORMADO;var DIGITO = new Array(10);DV_INFORMADO = CPF.substr(9, 2); for (I=0; I<=8; I++) {  DIGITO[I] = CPF.substr( I, 1);}POSICAO = 10;SOMA = 0;   for (I=0; I<=8; I++) {      SOMA = SOMA + DIGITO[I] * POSICAO;      POSICAO = POSICAO - 1;   }DIGITO[9] = SOMA % 11;   if (DIGITO[9] < 2) {        DIGITO[9] = 0;}   else{       DIGITO[9] = 11 - DIGITO[9];}POSICAO = 11;SOMA = 0;   for (I=0; I<=9; I++) {      SOMA = SOMA + DIGITO[I] * POSICAO;      POSICAO = POSICAO - 1;   }DIGITO[10] = SOMA % 11;   if (DIGITO[10] < 2) {        DIGITO[10] = 0;   }   else {        DIGITO[10] = 11 - DIGITO[10];   }DV = DIGITO[9] * 10 + DIGITO[10];   if (DV != DV_INFORMADO) {      alert('CPF inválido');      formulario.CPF.value = '';      formulario.CPF.focus();      return false;   } }</script>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Olha pessoal eu nao to conseguindo puxar minha função em Java Script :

 

Meu codigo esta aqui :::

 

<html>
<head>
<script type="text/javascript" src="../../funcoes.js"></script>
</head>

<body>
<form name="form1" method="post" action="../funcoes/adicionar.php">
<input name="CPF" type="text" class="input" id="Cnpj2" size="15" maxlength="19" onKeyPress="CaixaAlta(this)">
<input name="cad_cli" type="submit" class="input" id="cad_cli" value="Cadastrar" onClick="Verifica_CPF(this.form.CPF.value);">>
</form>
</body>
</html>

 

A função é a mesma que o nordi POSTOU aqui :

 

function Verifica_CPF(formulario) {var CPF = formulario.CPF.value; if (CPF == '') { alert('Este campo é de preenchimento obrigatório!'); return false;  }var POSICAO, I, SOMA, DV, DV_INFORMADO;var DIGITO = new Array(10);DV_INFORMADO = CPF.substr(9, 2); for (I=0; I<=8; I++) { DIGITO[I] = CPF.substr( I, 1);}POSICAO = 10;SOMA = 0;  for (I=0; I<=8; I++) {     SOMA = SOMA + DIGITO[I] * POSICAO;     POSICAO = POSICAO - 1;  }DIGITO[9] = SOMA % 11;  if (DIGITO[9] < 2) {       DIGITO[9] = 0;}  else{      DIGITO[9] = 11 - DIGITO[9];}POSICAO = 11;SOMA = 0;  for (I=0; I<=9; I++) {     SOMA = SOMA + DIGITO[I] * POSICAO;     POSICAO = POSICAO - 1;  }DIGITO[10] = SOMA % 11;  if (DIGITO[10] < 2) {       DIGITO[10] = 0;  }  else {       DIGITO[10] = 11 - DIGITO[10];  }DV = DIGITO[9] * 10 + DIGITO[10];  if (DV != DV_INFORMADO) {     alert('CPF inválido');     formulario.CPF.value = '';     formulario.CPF.focus();     return false;  } }

 

Se alguem puder me dar uma mao ai ?????

 

Vlw Falou

Compartilhar este post


Link para o post
Compartilhar em outros sites

Botão tava enviando paramentro errado

<input name="cad_cli" type="submit" class="input" id="cad_cli" value="Cadastrar" onClick="Verifica_CPF(document.form1.CPF.value);">

 

pequena mudança no código.

function Verifica_CPF(CPF) {if (CPF == '') {alert('Este campo é de preenchimento obrigatório!');return false; }var POSICAO, I, SOMA, DV, DV_INFORMADO;var DIGITO = new Array(10);DV_INFORMADO = CPF.substr(9, 2); for (I=0; I<=8; I++) {DIGITO[I] = CPF.substr( I, 1);}POSICAO = 10;SOMA = 0; for (I=0; I<=8; I++) {    SOMA = SOMA + DIGITO[I] * POSICAO;    POSICAO = POSICAO - 1; }DIGITO[9] = SOMA % 11; if (DIGITO[9] < 2) {      DIGITO[9] = 0;} else{     DIGITO[9] = 11 - DIGITO[9];}POSICAO = 11;SOMA = 0; for (I=0; I<=9; I++) {    SOMA = SOMA + DIGITO[I] * POSICAO;    POSICAO = POSICAO - 1; }DIGITO[10] = SOMA % 11; if (DIGITO[10] < 2) {      DIGITO[10] = 0; } else {      DIGITO[10] = 11 - DIGITO[10]; }DV = DIGITO[9] * 10 + DIGITO[10]; if (DV != DV_INFORMADO) {    alert('CPF inválido');    formulario.CPF.value = '';    formulario.CPF.focus();    return false; } }

Espero ter ajudado

Abraços T+ http://forum.imasters.com.br/public/style_emoticons/default/bye1.gif

Compartilhar este post


Link para o post
Compartilhar em outros sites

TO com uma duvida

 

Tenho esta função que valida o campo ;;;

 

<input name="submit" type="submit"  class="input"  value=" OK " onClick="ValidarCampo(document.cadastro.Nome.value,);">

 

function ValidarCampo(NCamp,NForm){  d = document.Nform;  if (d.NCamp.value == ""){  alert("O campo " + d.NCamp.name + " deve ser preenchido!");   d.NCamp.focus();   return false; } return true;}

Gostaria de saber como eu faço para validar todos os campos do meu Form e se este metodo funciona ???

 

Vlw pelas outras respostas...

Compartilhar este post


Link para o post
Compartilhar em outros sites

<input name="submit" type="button" class="input" value=" OK " onClick="enviaForm();">

 

ok = false;function enviaForm() {  frm = document.meuForm;  if (validator(frm.nome)) ok = true;  else alert('O campo nome não foi preenchido');  if (validator(frm.telefone)) ok = true;  else alert('O campo telefone não foi preenchido');  if (validator(frm.email)) ok = true;  else alert('O campo email não foi preenchido');  if (ok) document.meuForm.submit();}function validator(campo) {  if ( (campo.value == "") || (campo.value == " ") ) return false;  else return true;}

 

Espero que goste do exemplo

 

Abraços T+ http://forum.imasters.com.br/public/style_emoticons/default/bye1.gif

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.