Thursday, April 25, 2024
 Popular · Latest · Hot · Upcoming
182
rated 0 times [  187] [ 5]  / answers: 1 / hits: 38201  / 15 Years ago, fri, november 20, 2009, 12:00:00

im doing a form validation and I want to validate the input fields when on submit if error Im using jquery.scrollTo to go to error:



$('#form_inscripcion').submit(function() {
//se traen todos los inputs del formulario
var $inputs = $('#form_inscripcion :input');

$inputs.each(function() {
var encontro_error = validar($(this)); //uses dependence ok
if (encontro_error){
$.scrollTo( 'input#'+$(this).attr('id'), 800 ); //go to error
return false; // dont submit!... but seems not enter here :(
}
});

});


The problem is, when error returned doesnt cancel the submit, doesnt run return false; line.



It works OK when



<form id=form_inscripcion name=form method=post action=some onsubmit=return false>


But so, will never submits. I hope you understand me :) thanks :)


More From » jquery

 Answers
20

I would use e.preventDefault() like this:



$('#form_inscripcion').submit(function(e) { 
//se traen todos los inputs del formulario
var $inputs = $('#form_inscripcion :input');

$inputs.each(function() {
var encontro_error = validar($(this)); //uses dependence ok
if (encontro_error){
$.scrollTo( 'input#'+$(this).attr('id'), 800 ); //go to error
e.preventDefault(); // Cancel the submit
return false; // Exit the .each loop
}
});
});


Just be sure to supply the e parameter to the submit function call (first line in the code block).


[#98261] Wednesday, November 18, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucillemariselal

Total Points: 108
Total Questions: 97
Total Answers: 119

Location: Thailand
Member since Thu, May 6, 2021
3 Years ago
;