Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

128
Vistas
Jquery checking form

I am trying to check certain fields in this form and if they aren't correctly filled that it cannot submit. My problem is that when I submit with some fields not filled in it does give the error but it still goes to mail and if I then fill my form in correctly the error doesn't disappear and it doesn't focus like I ask.

<script>
            $(document).ready(function(){
                $("#submit").click(function(){
                   var naam = $("#naam").val();
                   var voornaam = $("#voornaam").val();
                   var bericht = $("#bericht").val();
                   if (naam == "" || voornaam == "" || bericht == ""){
                       $(".error").show();
                       if(naam == ""){
                           $("#naam").focus();
                       }
                       else if (voornaam == ""){
                           $("#voornaam").focus();
                       }
                       else{
                           $("#bericht").focus();
                       }
                   }
                   else{
                      $(".error").hide();
                      $("form").submit();
                       
                   }
                });
            });
        </script>
<form action="mailto:#" method="post" enctype="text/plain">
.error {
    margin-top: 10px;
    color: red;
    display: none;
}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Problem is that you are not preventing the actual submit of the form.

You can use event.preventDefault();

In the example below, you can see if I have added event.preventDefault(); inside the if statement where it fails. This will do so the form can't be submitted if the "validation" fails.

Demo

$(document).ready(function() {
  $("#submit").click(function(event) {
    var naam = $("#naam").val();
    var voornaam = $("#voornaam").val();
    var bericht = $("#bericht").val();
    if (naam == "" || voornaam == "" || bericht == "") {
      event.preventDefault();
      $(".error").show();
      if (naam == "") {
        $("#naam").focus();
      } else if (voornaam == "") {
        $("#voornaam").focus();
      } else {
        $("#bericht").focus();
      }
    } else {
      $(".error").hide();
      $("form").submit();
    }
  });
});
.error {
  margin-top: 10px;
  color: red;
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="mailto:nielsvervoort14@gmail.com" method="post" enctype="text/plain">
  <input id="naam" />
  <input id="voornaam" />
  <input id="bericht" />
  <button  id="submit">submit</button>
  <div class="error">error</div>
</form>

about 4 years ago · Juan Pablo Isaza Denunciar

0

If there is an error, the if statement needs to prevent the default behaviour. Pass the event object into the function and call its preventDefault.

  $("#submit").click(function(e){
$(".error").show();
e.preventDefault();

You might also try approaching the problem differently; using the <input type="text" required="required"> see https://devdocs.io/html/attributes/required

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use the event preventDefault method. just like that.

$(document).ready(function() {
   $('#submit').click(function(e){
       e.preventDefault();
    // or return false;
 });
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda