Ir para conteúdo

POWERED BY:

Arquivado

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

NetBoy16

Como paro o envio de form ?

Recommended Posts

Fala pessoal, beleza ?

 

Eu to fazendo uma validacao de formulario, no onbubmit coloquei funcoes do meu arquivo js, tipo, ele valida mas mesmo assim envia o form, os codigos estao assim:

 

//valida campos vazio
function ValidarOrcamento() {
   if((nome.value) == "") {
       alert("O campo nome não pode estar vazio");
       if(navigator.appName == "Microsoft Internet Explorer") { window.document.execCommand('Stop'); } else { window.stop(); }
   }
   if((email.value) == "") {
       alert("O campo email não pode estar vazio");
       if(navigator.appName == "Microsoft Internet Explorer") { window.document.execCommand('Stop'); } else { window.stop(); }
   }
   if((fone.value) == "") {
       alert("O campo fone não pode estar vazio");
       if(navigator.appName == "Microsoft Internet Explorer") { window.document.execCommand('Stop'); } else { window.stop(); }
   }
   if((evento.value) == "") {
       alert("O campo evento não pode estar vazio");
       if(navigator.appName == "Microsoft Internet Explorer") { window.document.execCommand('Stop'); } else { window.stop(); }
   }
   if((descricao.value) == "") {
       alert("O campo descricao não pode estar vazio");
       if(navigator.appName == "Microsoft Internet Explorer") { window.document.execCommand('Stop'); } else { window.stop(); }
   }
}
//valida campos vazio
function ValidarNews() {
   if((nomenews.value) == "") {
       alert("O campo nome não pode estar vazio");
       if(navigator.appName == "Microsoft Internet Explorer") { window.document.execCommand('Stop'); } else { window.stop(); };
   }
   if((emailnews.value) == "") {
       alert("O campo email não pode estar vazio");
       if(navigator.appName == "Microsoft Internet Explorer") { window.document.execCommand('Stop'); } else { window.stop(); }
   }
}
//valida campo email
function ValidaEmail(email)
       {
           var str = email.value;
           var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
           if(!filter.test(str)) {
             alert("Este endereço de e-mail não é válido!");
             if(navigator.appName == "Microsoft Internet Explorer") { window.document.execCommand('Stop'); } else { window.stop(); }
           }
       }

 

Tentei dessa forma mas nao deu certo, como faco pra parar caso algum campo esteja errado ?

Compartilhar este post


Link para o post
Compartilhar em outros sites

return false;

 

 

Já havia tentado asim tb mas n deu certo, olha o codigo:

 

//valida campos vazio
function ValidarOrcamento() {
   if((nome.value) == "") {
       alert("O campo nome não pode estar vazio");
       return false;
   }
   if((email.value) == "") {
       alert("O campo email não pode estar vazio");
       return false;
   }
   if((fone.value) == "") {
       alert("O campo fone não pode estar vazio");
       return false;
   }
   if((evento.value) == "") {
       alert("O campo evento não pode estar vazio");
       return false;
   }
   if((descricao.value) == "") {
       alert("O campo descricao não pode estar vazio");
       return false;
   }
}
//valida campos vazio
function ValidarNews() {
   if((nomenews.value) == "") {
       alert("O campo nome não pode estar vazio");
       return false;;
   }
   if((emailnews.value) == "") {
       alert("O campo email não pode estar vazio");
       return false;
   }
}
//valida campo email
function ValidaEmail(email)
       {
           var str = email.value;
           var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
           if(!filter.test(str)) {
             alert("Este endereço de e-mail não é válido!");
             return false;
           }
       }

Compartilhar este post


Link para o post
Compartilhar em outros sites

aparece algum erro no console ?

 

como você chama a função ?

Compartilhar este post


Link para o post
Compartilhar em outros sites

Eu chamo no onsubmit do form, olha o codigo html:

 

index.php

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
       <title>Studio MyDream</title>        
       <link rel="stylesheet" href="css/estilo.css" type="text/css"  />
       <script type="text/javascript" src="js/jquery-1.5.1.min.js"></script>
       <!-- JS BANNER-->
       <script type="text/javascript" src="js/jquery.cycle.all.min.js"></script>
       <script type="text/javascript" src="js/jquery.easing.1.3.js"></script>
       <script type="text/javascript" src="js/funcoesBanner.js"></script>
       <script type="text/javascript" src="js/funcoes.js"></script>


   </head>
   <body>
       <div id="container">
           <div id="topo">
               <div id="banner"><?php require("modulos/banner.php"); ?></div>
               <div id="menu"><?php require("modulos/menu.php"); ?></div>
           </div>
           <div id="conteudo">
               <div id="logo"><?php require("modulos/logo.php"); ?></div>
               <div id="orcamento">
                   <?php require("modulos/orcamento.php"); ?>
               </div>
               <div id="menufoto">
                   <img src="images/menufoto.png" alt="menu" border="0" usemap="#Map" />
                   <map name="Map" id="Map">
                     <area shape="rect" coords="19,34,136,314" href="fotografo-casamento-curitiba.php" />
                     <area shape="rect" coords="139,16,256,312" href="fotografo-festas-e-eventos-curitiba.php" />
                     <area shape="rect" coords="263,7,381,315" href="fotografo-produtos-curitiba.php" />
                     <area shape="rect" coords="386,8,504,316" href="fotografo-gastronomia-curitiba.php" />
                     <area shape="rect" coords="508,10,627,314" href="fotografo-books-curitiba.php" />
                   </map>
             </div>
               <div id="news">
                   <h3>NEWSLETTER</h3><br />
                   <p>Receba nossas informações, promoções e<br />novidades direto do seu e-mail</p><br />
                   <form name="formnews" id="formnews" method="post" action="news.php" onsubmit="ValidarNews(); ValidaEmail(formnews.emailnews)">
                       <input type="text" name="nomenews" id="nomenews" value="Nome" maxlength="50" style="width: 225px; height: 30px; background: url(images/formnews.png) no-repeat;" onfocus="this.value=''" /><br />
                       <input type="text" name="emailnews" id="emailnews" value="E-mail" maxlength="50" style="width: 225px; height: 30px; background: url(images/formnews.png) no-repeat; position: relative; top: -20px;" onfocus="this.value=''" /> 
                       <input type="submit" value="" style="width: 65px; height: 75px; background: url(images/newsok.png) no-repeat; position: relative; top: -40px;" />
                   </form>
               </div>
               <div id="anotacao">
                   <p>AQUI VAI O TEXTO<br />
                   AQUI VAI O TEXTO<br />
                   AQUI VAI O TEXTO<br />
                   AQUI VAI O TEXTO<br />
                   AQUI VAI O TEXTO<br /></p>
               </div>
               <div id="rsocial">
                   <?php require("modulos/rsocial.php"); ?>
               </div>
           </div>
           <div id="rodape">
               <?php require("modulos/rodape.php"); ?>
           </div>
       </div>
   </body>
</html>

 

 

orcamento.php

 

<h3>ORÇAMENTO</h3><br />
                   <p>Solicite agora um orçamento sem compromisso<br /> preenchendo os dados abaixo:</p><br />
                   <form name="form" id="form" method="post" action="php/formorcamento.php" onsubmit="ValidarOrcamento; ValidaEmail(form.email)">
                       <input type="text" name="nome" id="nome" value="Nome" maxlength="50" style="width: 310px; height: 32px; background: url(images/formnome.png)" onfocus="this.value=''" /><br />
                       <input type="text" name="email" id="email" value="E-mail" maxlength="50" style="width: 167px; height: 32px; background: url(images/formemail.png) no-repeat" onfocus="this.value=''" />
                       <input type="text" name="fone" id="fone" value="Telefone" style="width: 130px; height: 32px; background: url(images/formfone.png) no-repeat" onfocus="this.value=''" /><br />
                       <input type="text" name="evento" id="evento" value="Evento" maxlength="50" style="width: 310px; height: 32px; background: url(images/formevento.png)" onfocus="this.value=''" /><br />
                       <textarea name="descricao" id="descricao" maxlength="500" style="width: 310px; height: 150px; background: url(images/formdescricao.png)" onfocus="this.value=''" >Descrição</textarea><br />
                       <input type="submit" value="ENVIAR" class="submit" />
                   </form>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Ok, tenta assim:

 

onsubmit="return ValidarNews() && ValidaEmail(formnews.emailnews)">

Compartilhar este post


Link para o post
Compartilhar em outros sites

Ok, tenta assim:

 

onsubmit="return ValidarNews() && ValidaEmail(formnews.emailnews)">

 

Também nao deu :(

 

<h3>ORÇAMENTO</h3><br />
                   <p>Solicite agora um orçamento sem compromisso<br /> preenchendo os dados abaixo:</p><br />
                   <form name="form" id="form" method="post" action="php/formorcamento.php" onsubmit="return ValidarOrcamento() && ValidaEmail(form.email)">
                       <input type="text" name="nome" id="nome" value="Nome" maxlength="50" style="width: 310px; height: 32px; background: url(images/formnome.png)" onfocus="this.value=''" /><br />
                       <input type="text" name="email" id="email" value="E-mail" maxlength="50" style="width: 167px; height: 32px; background: url(images/formemail.png) no-repeat" onfocus="this.value=''" />
                       <input type="text" name="fone" id="fone" value="Telefone" style="width: 130px; height: 32px; background: url(images/formfone.png) no-repeat" onfocus="this.value=''" /><br />
                       <input type="text" name="evento" id="evento" value="Evento" maxlength="50" style="width: 310px; height: 32px; background: url(images/formevento.png)" onfocus="this.value=''" /><br />
                       <textarea name="descricao" id="descricao" maxlength="500" style="width: 310px; height: 150px; background: url(images/formdescricao.png)" onfocus="this.value=''" >Descrição</textarea><br />
                       <input type="submit" value="ENVIAR" class="submit" />
                   </form>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
       <title>Studio MyDream</title>        
       <link rel="stylesheet" href="css/estilo.css" type="text/css"  />
       <script type="text/javascript" src="js/jquery-1.5.1.min.js"></script>
       <!-- JS BANNER-->
       <script type="text/javascript" src="js/jquery.cycle.all.min.js"></script>
       <script type="text/javascript" src="js/jquery.easing.1.3.js"></script>
       <script type="text/javascript" src="js/funcoesBanner.js"></script>
       <script type="text/javascript" src="js/funcoes.js"></script>


   </head>
   <body>
       <div id="container">
           <div id="topo">
               <div id="banner"><?php require("modulos/banner.php"); ?></div>
               <div id="menu"><?php require("modulos/menu.php"); ?></div>
           </div>
           <div id="conteudo">
               <div id="logo"><?php require("modulos/logo.php"); ?></div>
               <div id="orcamento">
                   <?php require("modulos/orcamento.php"); ?>
               </div>
               <div id="menufoto">
                   <img src="images/menufoto.png" alt="menu" border="0" usemap="#Map" />
                   <map name="Map" id="Map">
                     <area shape="rect" coords="19,34,136,314" href="fotografo-casamento-curitiba.php" />
                     <area shape="rect" coords="139,16,256,312" href="fotografo-festas-e-eventos-curitiba.php" />
                     <area shape="rect" coords="263,7,381,315" href="fotografo-produtos-curitiba.php" />
                     <area shape="rect" coords="386,8,504,316" href="fotografo-gastronomia-curitiba.php" />
                     <area shape="rect" coords="508,10,627,314" href="fotografo-books-curitiba.php" />
                   </map>
             </div>
               <div id="news">
                   <h3>NEWSLETTER</h3><br />
                   <p>Receba nossas informações, promoções e<br />novidades direto do seu e-mail</p><br />
                   <form name="formnews" id="formnews" method="post" action="news.php" onsubmit="return ValidarNews() && ValidaEmail(formnews.emailnews)">
                       <input type="text" name="nomenews" id="nomenews" value="Nome" maxlength="50" style="width: 225px; height: 30px; background: url(images/formnews.png) no-repeat;" onfocus="this.value=''" /><br />
                       <input type="text" name="emailnews" id="emailnews" value="E-mail" maxlength="50" style="width: 225px; height: 30px; background: url(images/formnews.png) no-repeat; position: relative; top: -20px;" onfocus="this.value=''" /> 
                       <input type="submit" value="" style="width: 65px; height: 75px; background: url(images/newsok.png) no-repeat; position: relative; top: -40px;" />
                   </form>
               </div>
               <div id="anotacao">
                   <p>AQUI VAI O TEXTO<br />
                   AQUI VAI O TEXTO<br />
                   AQUI VAI O TEXTO<br />
                   AQUI VAI O TEXTO<br />
                   AQUI VAI O TEXTO<br /></p>
               </div>
               <div id="rsocial">
                   <?php require("modulos/rsocial.php"); ?>
               </div>
           </div>
           <div id="rodape">
               <?php require("modulos/rodape.php"); ?>
           </div>
       </div>
   </body>
</html>

 

debuguei eapareceu:

 

Hora: 4/4/2012 11:47:49

Erro: nome is not defined

Arquivo-fonte: http://localhost/studiomydream/js/funcoes.js

Linha: 3

Compartilhar este post


Link para o post
Compartilhar em outros sites

corrija.

 

=)

 

 

,-,

 

 

Mas eu acho estranho, o meu campo nome tem o name="nome", pq ele ta falando que ta indefinido ?

Compartilhar este post


Link para o post
Compartilhar em outros sites

pq o teu campo ter o name="nome", não significa absolutamente nada para o javascript.

 

nome no javascript é uma variavel. E você não definiu ela. Pronto, tão simples qnto isso.

Uma forma de definir seria:

 

var nome = document.getElementById('nome');

Agora sim, a variavel nome existe, e aponta para um objeto de id="nome"

 

 

Estude javascript básico.

Compartilhar este post


Link para o post
Compartilhar em outros sites

pq o teu campo ter o name="nome", não significa absolutamente nada para o javascript.

 

nome no javascript é uma variavel. E você não definiu ela. Pronto, tão simples qnto isso.

Uma forma de definir seria:

 

var nome = document.getElementById('nome');

Agora sim, a variavel nome existe, e aponta para um objeto de id="nome"

 

 

Estude javascript básico.

 

Ahhh ta, eu na sabia disso, sei quase nada de js, to mais é programando js via POG(programacao orientada a google), mas estudar javascript estava nos meus planos já, só esperar receber e vou comprar o lviro de js do maujor :lol:

 

Mas ainda nao ta dando certo :( , olha meu codigo como que ficou:

 

//valida campos vazio
function ValidarOrcamento() {
   var nome = document.getElementById('nome');
   var email = document.getElementById('email');
   var fone = document.getElementById('fone');
   var evento = document.getElementById('evento');
   var descricao = document.getElementById('descricao');

   if((nome.value) == "") {
       alert("O campo nome não pode estar vazio");
       return false;
   }
   if((email.value) == "") {
       alert("O campo email não pode estar vazio");
       return false;
   }
   if((fone.value) == "") {
       alert("O campo fone não pode estar vazio");
       return false;
   }
   if((evento.value) == "") {
       alert("O campo evento não pode estar vazio");
       return false;
   }
   if((descricao.value) == "") {
       alert("O campo descricao não pode estar vazio");
       return false;
   }
}
//valida campos vazio
function ValidarNews() {
   var nomenews = document.getElementById('nomenews');
   var emailnews = document.getElementById('emailnews');

   if((nomenews.value) == "") {
       alert("O campo nome não pode estar vazio");
       return false;;
   }
   if((emailnews.value) == "") {
       alert("O campo email não pode estar vazio");
       return false;
   }
}
//valida campo email
function ValidaEmail(email)
       {
           var str = email.value;
           var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
           if(!filter.test(str)) {
             alert("Este endereço de e-mail não é válido!");
             return false;
           }
       }

 

nada ainda :cry:

 

oque será que acontece? alguém por favor pode me ajudar ? :(

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.