I’m trying to validate this form with jQuery validate plugin, but it doesn’t stop javascript code once it finds errors, so the form is sent to the DB and only then the errors are shown. What could i do to chain those events properly?
$("#salvar-cur").live("click", function () {
var validator = $("#frm_curriculo").validate({
rules: {
cur_nome: {
required: true
}
//other options
},
messages: {
cur_nome: {
required: "*"
}
//other options
}
});
if (validator.numberOfInvalids() == 0) {
alert("Success!")
var params = $("#frm_curriculo").serialize();
$.ajax({
type: "POST",
url: "controller/ctrl_curriculo.php",
data: "acao=inserir&" + params,
dataType: "html",
contentType: "application/x-www-form-urlencoded",
success: function () {
$("#frm_curriculo")[0].reset();
}
});
}
});
First off, you are re-adding the validator on each click. You should really do that outside of the event handler.
Also, instead of checking
validator.numberOfInvalids(), try checking$("#frm_curriculo").valid(), this will validate the form and returntrueorfalse.Another note, if
#salvar-curis a submit button, you’re gonna want to prevent its default action.