1
resposta

Jquery, validar antes do submit

Pessoal

Estou com um código que possui validação em Jquery de todos os campos. Após a validação o código inclui os itens em uma tabela. Até aí funcionava tudo certo.

Após transformar tudo em FORM e incluir um SUBMIT e um ACTION, que insere os itens no banco de dados, a coisa desandou. A validação ocorre, a mensagem MODAL aparece, mas inclui no BD de qualquer forma... Alguma ideia do problema?

Estou puxando os dados em JSP (o servidor só aceita JSP).

Papel

<form id="descarte-papel" style="margin-top:25px;" action="grava_papel.jsp">

    <fieldset>
              <label style="font-size:150%;"> <b>Peso (kg):</b> </label>     
              <input name="peso_papel" id="peso_papel" class="peso"  type="text" placeholder="00,00" />

              <label style="font-size:150%; margin-left: 1%;"> <b>Data de saida:</b> </label>     
              <input name="data_s_papel" id="data_s_papel" class="date" type="text" placeholder="00/00/0000" 
              />

              <label style="font-size:150%; margin-left: 1%;"> <b>Recebedor:</b> </label>     
              <select name="recebedor_papel" id="recebedor_papel" class="recebedor" onChange="mascara('#recebedor_papel','#cnpj_papel');">  
                  <option value=" "></option>
                  <option value="Coleta Pública Convencional">Coleta Pública Convencional</option>
                  <option value="Coleta Pública Seletiva">Coleta Pública Seletiva </option>
                  <option value="Cooperativa de Catadores">Cooperativa de Catadores</option>
                  <option value="Empresa de Reciclagem">Empresa de Reciclagem</option>
                  <option value="Outros Recebedores">Outros Recebedores</option>
              </select>  



              <label style="font-size:150%; margin-left: 1%;"> <b> CNPJ:</b> </label>     
              <input name="cnpj_papel"  id="cnpj_papel" type="text"  class="cnpj" placeholder="00.000.000/0000-00 "  style="font-size: 16px; " disabled/>

              <span id="r-msg-obg" class="msg-obg" style="position:absolute; right: 125; top:2;  display: none;"> * Preenchimento obriatório </span>



              <button type="submit" onClick="AddTableRow('#details-table-papel','#peso_papel','#data_s_papel','#recebedor_papel','#cnpj_papel')" class=" btn botao">Incluir </button>
    </fieldset>


    <div>

                        <table id="details-table-papel" class="table table-sm details-table-residuo fixed_header" style="max-width:89.5%; min-width:88.5%; margin-top: 1%; margin-bottom:0; ">            
                          <thead >           
                          <tr style="background-color:034072; color: rgb(255, 255, 255); ">
                            <th>Peso (kg)</th>           
                            <th>Data de Saída</th>           
                            <th>Recebedor</th>                                    
                            <th >CNPJ</th>
                            <th >Registrado em</th>           
                            <th> </th>         

                          </tr>
                          </thead>
                          <tbody>         







                            </tbody>    


                            </table>
                          </div>

  <span class="subtitulo-conteudo" > Total registrado no mês:  <span style="font-size:120%;"> $$$$$$</span></span> 
</form>
1 resposta

Olá Willian! Segue um exemplo de código, talvez te ajude.

<html>
<head>
  <style>

  p { margin:0; color:blue; }
  div,p { margin-left:10px; }
  span { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
  <p>Type 'correct' to validate.</p>
  <form action="javascript:alert('success!');">
    <div>
      <input type="text" />

      <input type="submit" />
    </div>
  </form>
  <span>
</span>

<script>


$("form").submit(function() {
  if ($("input:first").val() == "correct") {
    $("span").text("Validated...").show();
    return true;
  }
  $("span").text("Not valid!").show().fadeOut(1000);
  return false;
});
</script>

</body>
</html>

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software