0

I trying to validate a form with jquery, but the function "validacion()" doesn't work.

I'm doing it on apache and eclipse.

Form

<form action="destino.html" method="post" id="formularioDarAlta" onsubmit="return validacion();">
<fieldset>
<legend>Formulario alta libro</legend>
...
    <p>
        <input type="submit" value="Insertar"/>         
    </p>
</fieldset>
</form>

Script

<script>
function validacion() 
{   
    var isbn = $('#isbn').val();
    var titulo = $('#titulo').val();
    var categoria = $('#categoria').val();
    var respuesta = true;

    if(isbn.value == "")
    {
        alert("Debe ingresar ISBN");
        respuesta = false;
    }       
    if(titulo.value == "")
    {
        alert("Debe ingresar título");
        respuesta = false;
    }           
    if(categoria.value == "")
    {
        alert("Debe ingresar categoría");
        respuesta = false;
    }       
    return respuesta;           
}
</script>
bugwheels94
  • 30,681
  • 3
  • 39
  • 60
  • possible duplicate of [On submit form, return false not working](http://stackoverflow.com/questions/18480001/on-submit-form-return-false-not-working) – TaoPR Jun 17 '15 at 14:55
  • It's not a dupe, but a typo. Change `isbn.value` --> `isbn`, `titulo.value` --> `titulo`, `categoria.value` --> `categoria`. – Drakes Jun 17 '15 at 14:59

1 Answers1

2

You need not use isbn.value since you already do $('#isbn').val() Here is a fiddle with updated code http://jsfiddle.net/674hv53e/1/

 if(isbn == "")
    {
        alert("Debe ingresar ISBN");
        respuesta = false;
    }   
meteor
  • 2,518
  • 4
  • 38
  • 52