0

This form allways submit, never javascript stop with the errors. What is wrong?

thanks in advance ;)

I put the onsubmit return valida(this).... and the javascript code seems well

<form name="formulario" id="formulario" method="POST" action="<%=request.getContextPath()%>/altaanuncio" onSubmit="return valida(this)">


function valida(f) {

        if (f.marca.selectedIndex==0){
            $('#errores').html('Seleccione la marca.');
            f.marca.focus();
         return false;
    }

        else if(f.garantia.value == ""){
            $('#errores').html('Introduzca meses de garantía');
            f.garantia.focus();
                return false;
        }

        else if(f.pvpofertado.value == ""){
            $('#errores').html('Introduzca el precio del coche');
            f.pvpofertado.focus();
                return false;

        }

     return true;
}
3
  • Did you try to put valida(this) instead of return valida(f) ? Commented Oct 12, 2011 at 16:33
  • Ok what server-side language/framework are you using? Commented Oct 12, 2011 at 16:53
  • @lc2817 in plain JS it of course MUST be return valida(this) Commented Oct 12, 2011 at 17:01

2 Answers 2

1

I think you're trying to call jQuery functions on plain JS elements. Try this:

// When the DOM is ready:
$(document).ready(function() {

    // Attach submit event listener to form (== onSubmit)
    $('#formulario').submit(function(event) {

        // Get the jQuery object for the 'marca' field:
        var marca = event.target.children('#marca');

        if (marca.attr('selectedIndex') === 0) {
            $('#errores').text('Seleccione la marca.');
            marca.focus(); // <--this now works because it's a jQuery object.
            event.preventDefault(); // instead of return false;
        }
    });
});

<form id="formulario" />
Sign up to request clarification or add additional context in comments.

6 Comments

Ah, @mplungjan beat me to it.
I know whats happen. I HAVE AJAX SUBMIT!!!! Then it submit always, are there any mode of call the function and stop the submit?
But I have a very slow connection today so it takes me forever to fix typos ;)
event.preventDefault() should stop the submit event from performing its usual action (i.e. submitting the form via the regular way). If you're using AJAX to submit anyway, this should probably be at the top of your submit() event handler. See @mplungjan's answer.
@PPvG what would be the most elegant way of not continuing the validation after each focus? return false?
|
1

You obviously use jQuery so use jQuery all the way

There are validation plugins to be found here: http://zoomzum.com/useful-jquery-form-validation/

And post here: http://api.jquery.com/jQuery.post/

<html>
<head>
<script type="text/javascript" src="jquery.latest.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  $("#formulario").bind("submit",function(e) {
    var f = this; // or $(this) but then you need to test .val() and use find() or children
    if (f.marca.selectedIndex==0){
       $('#errores').html('Seleccione la marca.');
       f.marca.focus();
       e.preventDefault();
    }
.
.
.
.
   }
   $.post($(this).attr('action'),$(this).serialize(),
     success: function( response )  {
       console.log( response );
     }
   });
   e.preventDefault(); // cancel the actual submit       

  });
});
</script>
</head>
<body>
<form name="formulario" id="formulario" method="POST" action="<%=request.getContextPath()%>/altaanuncio">

2 Comments

I know whats happen. I HAVE AJAX SUBMIT!!!! Then it submit always, are there any mode of call the function and stop the submit?
Sure. Please see update. I will add an example but my internet is SLOOOW right now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.